Hi I've been trying and haven't found a solution for this yet and hopefully some of you can give me a direction to solve this problem. I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' How can I convert it to 32 chars String where each byte is $AB $23 $FF $FE .... ? Even if I do it manually, can I store the hex(non ASCII) to String? Thanks for looking!!
![]() |
-1 |
![]() |
edwin wrote: > I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' > How can I convert it to 32 chars String where each byte is $AB $23 $FF > $FE .... ? The RTL has several overloaded HexToBin() functions available in the System.Classes unit. > Even if I do it manually, can I store the hex(non ASCII) to String? Why do you need them in a String? String uses Unicode characters, but what you are asking for is not Unicode data. You should use TBytes (or other suitable binary container) instead, eg: {code} var Hex: String; Bytes: TBytes; begin Hex := 'AB23FFFE.....'; SetLength(Bytes, Length(Hex) div 2); HexToBin(PChar(Hex), Bytes[0], Length(Bytes)); ... end; {code} If you absolutely need a String containing characte representations of the raw bytes, you can do this: {code} var Hex: String; Bytes: TBytes; I: Ineger; S: String; begin Hex := 'AB23FFFE.....'; SetLength(Bytes, Length(Hex) div 2); HexToBin(PChar(Hex), Bytes[0], Length(Bytes)); SetLength(S, Length(Bytes)); for I := 0 to Length(Bytes)-1 do S[I+1] := Char(Bytes[I]); ... end; {code} -- Remy Lebeau (TeamB)
![]() |
0 |
![]() |
Thank you so much Remy. I'm going to give it a try. > {quote:title=Remy Lebeau (TeamB) wrote:}{quote} > edwin wrote: > > > I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' > > How can I convert it to 32 chars String where each byte is $AB $23 $FF > > $FE .... ? > > The RTL has several overloaded HexToBin() functions available in the System.Classes > unit. > > > Even if I do it manually, can I store the hex(non ASCII) to String? > > Why do you need them in a String? String uses Unicode characters, but what > you are asking for is not Unicode data. You should use TBytes (or other > suitable binary container) instead, eg: > > {code} > var > Hex: String; > Bytes: TBytes; > begin > Hex := 'AB23FFFE.....'; > SetLength(Bytes, Length(Hex) div 2); > HexToBin(PChar(Hex), Bytes[0], Length(Bytes)); > ... > end; > {code} > > If you absolutely need a String containing characte representations of the > raw bytes, you can do this: > > {code} > var > Hex: String; > Bytes: TBytes; > I: Ineger; > S: String; > begin > Hex := 'AB23FFFE.....'; > SetLength(Bytes, Length(Hex) div 2); > HexToBin(PChar(Hex), Bytes[0], Length(Bytes)); > SetLength(S, Length(Bytes)); > for I := 0 to Length(Bytes)-1 do > S[I+1] := Char(Bytes[I]); > ... > end; > {code} > > -- > Remy Lebeau (TeamB)
![]() |
0 |
![]() |