cnpack / cnvcl

CnPack VCL Components
http://www.cnpack.org
335 stars 102 forks source link

cnVCL whether add the function to conversion floating number to hexadecimal string ? #26

Closed sxbug closed 1 year ago

sxbug commented 1 year ago

cnFloat could conversion from 12.5 to C.8. Can it be converted to IEEE754's 41 48 00 00. I think this function is used a lot. thank you!

shanzhashu commented 1 year ago

You can use function DataToHex(InData: Pointer; ByteLength: Integer; UseUpperCase: Boolean = True): string; in CnNative.pas to directly convert the memory content of Float number to a hex string.

given

var A: Single; begin Caption := DataToHex(@A, SizeOf(A)); end;

sxbug commented 1 year ago

thanksthanks

sxbug commented 1 year ago

I only found the function: #1#2#3#4 <---> '1234'. But I want a function:12.5 <---> '41 48 00 00'

Thanks

shanzhashu commented 1 year ago

Could you use my code piece and check the result?

var A: Single; begin A := 12.5; Result := DataToHex(@A, SizeOf(A)); end;

In above: function DataToHex(InData: Pointer; ByteLength: Integer; UseUpperCase: Boolean = True): string; is in cnvcl\Source\Crypto\CnNative.pas

sxbug commented 1 year ago

Thank you for your guidance。

I misunderstand at first. Now I also want to know which function could convert # $41 48 00 00 to 12.5 , hex-->single。

Happy Chinese new year!

shanzhashu commented 1 year ago

There's also a function HexToData in CnNative.pas

Note:

  1. Space Chars in String should be removed before calling this function.
  2. Notice the BigEndian/Little Endian byte order after conversion.
sxbug commented 1 year ago

These compoments is very good, but there are a few documents。Thanks