BinHexMethods (C++)
Description
The purpose of this example is to illustrate the BinToHex and HexToBin methods from the Classes unit. This code will convert a binary value to its hexadecimal representation and back.
Code
void __fastcall TForm1::btnToHexClick(TObject *Sender)
{
UnicodeString str1, str2;
/* Store the text in the memo to a String variable. */
str1 = Memo1->Lines->Text;
/* Set the length of the String to hold the conversion. */
str2.SetLength(str1.Length() * 4);
/* Call the binary to hexadecimal conversion procedure. */
BinToHex(str1.c_str(), str2.c_str(), str1.Length() * sizeof(WideChar));
/* Put the results in Memo2. */
Memo2->Lines->Text = str2.c_str();
}
void __fastcall TForm1::btnToStringClick(TObject *Sender)
{
UnicodeString str1, str2;
/* Store the text in the memo to a String variable. */
str1 = Memo2->Lines->Text;
/* Set the length of the String to hold the conversion. */
str2.SetLength(str1.Length() / 4);
/* Call the hexadecimal to binary conversion procedure. */
HexToBin(str1.c_str(), str2.c_str(), str1.Length() / sizeof(WideChar));
/* And output the results to Memo1. */
Memo1->Lines->Text = str2;
}
Uses
- System.Classes.BinToHex ( fr | de | ja )
- System.Classes.HexToBin ( fr | de | ja )