开发者

convert hex formatted data into string

I have some data formatted in hex (stored in byte) that i need to convert to string, the data should be like this

13 61 23 45 67 8F FF

and expected output

13612345678FFF

i know Chr() or IntToStr()开发者_StackOverflow中文版 function will not work since this data not actual hex code but string/number formatted in hex, so anyone has some suggestion for this?


This is for converting a dynamic array of bytes to a hex string:

function BytesToHex(aSource: TBytes): string;
begin
  SetLength(Result, Length(aSource) * 2);
  if Length(aSource) > 0 then
    BinToHex(aSource[0], PChar(Result), Length(aSource));
end;

If your source bytes are not in a dynamic array, you'll have to adjust the code slightly, but it should give you a general idea of how to do it.


With the example that you've provided, why don't you just remove the spaces?

s := '13 61 23 45 67 8F FF';

stripped := StrUtils.ReplaceStr(s,' ','');

if your hex string is not too long, you can get it as a number like this:

MyInt64 := StrToInt64('$' + stripped);

Check out HexToBin() if you want to get your hex string as an array of bytes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜