Delphi Programming RTL Reference|By Category|Alphabetically|By Unit
procedure BinToHex(BinValue, HexValue: PChar; BinBufSize: Integer);
Converts a binary value to its hexadecimal representation.
HexValue returns a null-terminated string that represents the (binary) value of BinValue as a hexadecimal number. BinBufSize is the size of BinValue. HexValue needs to point to a sequence of characters that has at least 2*BinBufSize bytes because each hexadecimal character represents two bytes.
// drop 3 TLabel components on a Form
// and assign the code to form create
var
E: Extended;
//Make sure there is room for null terminator
Buf: array[0..SizeOf(Extended) * 2] of Char;
begin
E := Pi;
Label1.Caption := Format('Pi starts off as %.15f', [E]);
BinToHex(@E, Buf, SizeOf(E));
//Slot in the null terminator for the PChar, so we can display it easily
Buf[SizeOf(Buf) - 1] := #0;
Label2.Caption := Format('As text, the binary contents of Pi look like %s', [Buf]);
//Translate just the characters, not the null terminator
HexToBin(Buf, @E, SizeOf(Buf) - 1);
Label3.Caption := Format('Back from text to binary, Pi is now %.15f', [E]);
end;
|
Base Conversions, and more
Routines for converting Int to Bin, Int to Hex, Int to Roman and vice versa.
HexToBin,
|
Free Delphi code snippet inside every Delphi Newsletter! |
|
|
|
Got some code to share? Got a question? Need some help? |
|
|