Auxiliary functions, from projects.
Let's say there is a string input.
#50#54#51#171#56#53#173#65#55#52#172#64#57#58#59#60#61#62#63#175#176#177#178#179#180#165#166#167#168#169#170#181
String -> byte array
type
ArrayOfByte = array of byte;
function Str2Hex(s: string): ArrayOfByte;
var
i: Integer;
sLength: Integer;
begin
sLength := Length(s);
if (sLength > 0) then
begin
SetLength(Result, sLength);
for i := 0 to High(Result) do
Result[i] := Ord(S[i + 1]);
end;
end;
We obtained a byte array.
(50, 54, 51, 171, 56, 53, 173, 65, 55, 52, 172, 64, 57, 58, 59, 60, 61, 62, 63, 175, 176, 177, 178, 179, 180, 165, 166, 167, 168, 169, 170, 181)
Byte -> StrHex (Hexadecimal representation)
function ByteToHex(InByte: byte): shortstring;
const
Digits: array[0..15] of char = '0123456789ABCDEF';
begin
result := digits[InByte shr 4] + digits[InByte and $0F];
end;
function Hex2StrHex(aBuf: ArrayOfByte): string;
var
i: Integer;
begin
Result := '';
for i := Low(aBuf) to High(aBuf) do
Result := Result + ByteToHex(aBuf[i]); //The Format('%x', [aBuf[i]]) doesn't work quite correctly in this case; it converts 7 to 7 instead of 07.
end;
Hexadecimal representation
'323633AB3835AD413734AC40393A3B3C3D3E3FAFB0B1B2B3B4A5A6A7A8A9AAB5'
Or, by adding spaces, we obtain
32 36 33 AB 38 35 AD 41 37 34 AC 40 39 3A 3B 3C 3D 3E 3F AF B0 B1 B2 B3 B4 A5 A6 A7 A8 A9 AA B5
Hexadecimal representation -> bytes
function StrHex2Hex(sHex: string): ArrayOfByte;
var
i: Integer;
sHexLength: Integer;
sHexWithoutSpaces: string;
begin
sHexWithoutSpaces := '';
for i := 1 to Length(SHex) do
if SHex[i] <> ' ' then
sHexWithoutSpaces := sHexWithoutSpaces + SHex[i];
sHexLength := Length(sHexWithoutSpaces);
if (sHexLength > 0) and (sHexLength mod 2 = 0) then
begin
SetLength(Result, sHexLength div 2);
for i := 0 to High(Result) do
Result[i] := StrToInt('$' + Copy(sHexWithoutSpaces, i * 2 + 1, 2));
end;
end;
We obtained a byte array.
(50, 54, 51, 171, 56, 53, 173, 65, 55, 52, 172, 64, 57, 58, 59, 60, 61, 62, 63, 175, 176, 177, 178, 179, 180, 165, 166, 167, 168, 169, 170, 181)
Alternatively, there is another option using standard Delphi functions.
function ConvertHexStringToBinary(const strInHex: ansistring; out errorMsg: string): ansistring;
var
noSpaces: ansistring;
begin
noSpaces:= LowerCase(AnsiReplaceText(strInHex, ' ', ''));
SetLength(Result, Length(NoSpaces) div 2);
if (Length(Result) = HexToBin(PChar(noSpaces), PChar(Result), Length(Result))) then
errorMsg:= ''
else
errorMsg:= 'Hex To bin returns a number of converted symbols';
end;
Byte array -> string
function Hex2Str(aBuf: array of byte): string;
var
i: integer;
bufLength: integer;
begin
bufLength := Length(aBuf);
Result := '';
for i := 0 to BufLength - 1 do
begin
Result := Result + Chr(aBuf[i]);
end;
end;
The string was returned.
'263«85A74¬@9:;<=>?¯°±²³´¥¦§¨©ªµ'
Let's convert it to the numerical representation of the string.
function StrToOrdStr(aChrStr: string): string;
var
i: integer;
begin
Result := '';
for i := 1 to Length(aChrStr) do
Result := Result + '#' + IntToStr(Ord(aChrStr[i]));
end;
The result.
'#50#54#51#171#56#53#173#65#55#52#172#64#57#58#59#60#61#62#63#175#176#177#178#179#180#165#166#167#168#169#170#181'