Author: Olívio Moura
Undocumented BinToHex and HexToBin ?
Answer:
procedure BinToHex(Buffer, Text: PChar; BufSize: Integer);
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer;
Many VCL and Windows API routines require TPoint or TRect records. To save
declaring local variables and filling in the fields, you can use the set of
routines declared in this unit. Point takes an X and Y co-ordinate, and produces a
TPoint. Rect takes the left, top, right and bottom co-ordinates of a rectangle and
manufactures a TRect record. Bounds is very similar to Rect but takes left, top,
width and height information.
In Delphi 4 (and later), you will also find the undocumented BinToHex and HexToBin.
These translate between binary data and a textual representation of it. They exist
in all versions of Delphi, but Delphi 4 is the first to surface them outside the
unit.
For example, an Extended variable is 10 bytes in size. Each byte is represented as
two hexadecimal characters, so 20 characters are needed to display its contents in
pure text. Listing 8 translates the 10 bytes of an Extended into a string (PChar)
and then back again.
1 Using BinToHex and HexToBin
2
3 //Buffer is binary data,
4 //Text is target text buffer (assumed to be big enough),
5 //BufSize is size of binary data block
6 //procedure BinToHex(Buffer, Text: PChar; BufSize: Integer);
7
8 //Text is textual representation of binary data,
9 //Buffer is target binary data buffer
10 //BufSize is size of textual data buffer
11 //function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer;
12
13 procedure TForm1.Button1Click(Sender: TObject);
14 var
15 E: Extended;
16 //Make sure there is room for null terminator
17 Buf: array[0..SizeOf(Extended) * 2] of Char;
18 begin
19 E := Pi;
20 Label1.Caption := Format('E starts off as %.15f', [E]);
21 BinToHex(@E, Buf, SizeOf(E));
22 //Slot in the null terminator for the PChar, so we can display it easily
23 Buf[SizeOf(Buf) - 1] := #0;
24 Label2.Caption := Format('As text, the binary contents of E look like %s', [Buf]);
25 //Translate just the characters, not the null terminator
26 HexToBin(Buf, @E, SizeOf(Buf) - 1);
27 Label3.Caption := Format('Back from text to binary, E is now %.15f', [E]);
28 end;
|