HexEncoding (C++)
From RAD Studio Code Examples
Language:
Description
This example demonstrates the creation of a class named THexEncoding, derived from the TEncoding class. The methods you need to implement are GetByteCount, GetBytes, GetCharCount, GetChars, GetMaxByteCount, GetMaxCharCount, and GetPreamble.
Code
class THexEncoding:public TEncoding { protected: virtual int __fastcall GetByteCount(System::WideChar * Chars, int CharCount) = 0 /* overload */; virtual int __fastcall GetBytes(System::WideChar * Chars, int CharCount, System::PByte Bytes, int ByteCount) = 0 /* overload */; virtual int __fastcall GetCharCount(System::PByte Bytes, int ByteCount) = 0 /* overload */; virtual int __fastcall GetChars(System::PByte Bytes, int ByteCount, System::WideChar * Chars, int CharCount) = 0 /* overload */; public: virtual int __fastcall GetMaxByteCount(int CharCount) = 0 ; virtual int __fastcall GetMaxCharCount(int ByteCount) = 0 ; virtual TBytes __fastcall GetPreamble(void) = 0 ; }; int __fastcall THexEncoding::GetByteCount(System::WideChar * Chars, int CharCount) { /* The result is the number of characters multiplied by 2 times the size of the wchar_t type. */ return (CharCount * sizeof(wchar_t) * 2); } int __fastcall THexEncoding::GetBytes(System::WideChar * Chars, int CharCount, System::PByte Bytes, int ByteCount) { /* The BinToHex method converts a binary value to its hexadecimal representation. */ BinToHex(Chars, PAnsiChar(Bytes), CharCount * sizeof(wchar_t) * 2); /* This function returns nothing. */ } int __fastcall THexEncoding::GetCharCount(System::PByte Bytes, int ByteCount) { /* The result of this funcion is the number of bytes divided by 2. */ return (ByteCount / 2); } int __fastcall THexEncoding::GetChars(System::PByte Bytes, int ByteCount, System::WideChar * Chars, int CharCount) { /* The HexToBin method converts a hexadecimal value to its binary representation. */ HexToBin(PAnsiChar(Bytes), Chars, ByteCount / sizeof(wchar_t)); /* This function returns nothing. */ } int __fastcall THexEncoding::GetMaxByteCount(int CharCount) { /* The result of this function is the number of chars multiplied by 2. */ return (CharCount * 2); } int __fastcall THexEncoding::GetMaxCharCount(int ByteCount) { /* The result is the number of bytes divided by 2. Add a 1 for safety reasons. */ return ((ByteCount / 2) + 1); } TBytes __fastcall THexEncoding::GetPreamble(void) { /* The result is nil because the use of a preamble is not supported. */ return TBytes(); }
Uses
- System.SysUtils.TEncoding.GetByteCount ( fr | de | ja )
- System.SysUtils.TEncoding.GetBytes ( fr | de | ja )
- System.SysUtils.TEncoding.GetCharCount ( fr | de | ja )
- System.SysUtils.TEncoding.GetChars ( fr | de | ja )
- System.SysUtils.TEncoding.GetMaxByteCount ( fr | de | ja )
- System.SysUtils.TEncoding.GetMaxCharCount ( fr | de | ja )
- System.SysUtils.TEncoding.GetPreamble ( fr | de | ja )
- System.Classes.BinToHex ( fr | de | ja )
- System.Classes.HexToBin ( fr | de | ja )