I'm having an issue in converting HEX to a string. Here's the routine I use:

I'm having an issue in converting HEX to a string. Here's the routine I use:

function HexStrToStr(const HexStr: string): string;
var
tmp: AnsiString;
begin
SetLength(tmp, Length(HexStr) div 2);
HexToBin(PWideChar(HexStr), @tmp[1], Length(tmp));
result := tmp;
end;

For the most part this has done me well over the years but at the moment I am exchanging values with a third party and finding some characters don't translate the same way.

So, for example: HEX 82 converts to character 8218 in Delphi (single low-9 quotation mark - "‚" ) but in RapidTables and CodeBeautify it converts to character 130 which is what 82 is equal to (a non visible character btw).

HEX 97 converts to character 8212 ("—").

Any ideas?
https://www.rapidtables.com/convert/number/hex-to-ascii.html

Comments

  1. you need to set the codepage of tmp...or request an UTF8 hexa dump

    ReplyDelete
  2. maybe change tmp: AnsiString; to tmp: UT8String; ?

    ReplyDelete
  3. Hex to binary is clear.
    How you interpret this binary to characters is an other story.

    ReplyDelete
  4. Result := tmp
    will do a conversion from some ANSI character set to UTF-16

    ReplyDelete
  5. You need to decide what text encoding to use to decode the binary to text. Which encoding do you want to use?

    ReplyDelete

Post a Comment