Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Delphi Undocumented: Using BinToHex and HexToBin Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 4.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Olívio Moura

Undocumented BinToHex and HexToBin ?


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 

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 
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);
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;
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;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC