BinHexMethods (C++)

From RAD Studio Code Examples
Jump to: navigation, search

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 Tfrm1::btToHexClick(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.w_str(), str2.w_str(), str1.Length() * sizeof(wchar_t));
	/* Put the results in Memo2. */
	Memo2->Lines->Text = str2.w_str();
}

void __fastcall Tfrm1::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(wchar_t));
	/* And output the results to Memo1. */
	Memo1->Lines->Text = str2;
}

Uses