开发者

Delphi: Convert a TByte into its corresponding Hex-Value in AnsiString

I'm trying to output the value of a TByte as its corresponding hexadezimal representation in an AnsiString.

Example: TByte is 0x4F AnsiString: "4F" (two characters, a 4 and an F)

I know of the StringOf function, but that converts to the "mapped" character.

Is there any decent function build in or does somebody has a good idea how to implement this fast?

Thank you very开发者_如何转开发 much in advance :)


I'd cast it to a Byte and feed it to SysUtils.IntToHex.


The Format Function has been present since Turbo Pascal days and the SysUtils Hex functions were not available.

So for any version of Delphi before D2010, use the format command:

lByteAsHex := format('%.2x',[AByte]);

The .2 Precision specifier ensures that there is always leading zeros.

The following line:

lIntAsHex = format('%x',[AInt]);

handles any size integer (including int64) but will not put leading zeros, so format(%x,[10]) will return 'A' whereas format('%.2x',[10] will return '0A'

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜