// Value: a binary string, for example, 11000111
Function convertb1_str (value: ansistring): ansistring; // converts binary data to a string.
VaR
Temphex: ansistring;
I, tempint: integer;
Begin
Result: = '';
If trim (value) = ''then exit;
Temphex: = bintohexeachother (value, true); // convert binary to hexadecimal
I: = 0;
Repeat
Begin
I: = I + 1;
Tempint: = hexchartoint (temphex [I]);
I: = I + 1;
Tempint: = tempint * 16 + hexchartoint (temphex [I]);
// Convert the two hexadecimal numbers to a decimal number.
Result: = Result + ansichar (tempint); // convert it to an ascii code
End;
Until I> = length (temphex)
End;
// Convert a binary string to a hexadecimal string or the opposite
Function bintohexeachother (valuea: string; Action: Boolean): string;
VaR
Valuearray1: array [0 .. 15] of string [4];
Valuearray2: array [0 .. 15] of char;
I: Random int;
Begin
// Array Initialization
Valuearray1 [0]: = '000000'; valuearray1 [1]: = '000000'; valuearray1 [2]: = '000000 ';
Valuearray1 [3]: = '000000'; valuearray1 [4]: = '000000'; valuearray1 [5]: = '000000 ';
Valuearray1 [6]: = '000000'; valuearray1 [7]: = '000000'; valuearray1 [8]: = '000000 ';
Valuearray1 [9]: = '000000'; valuearray1 [10]: = '000000'; valuearray1 [11]: = '000000 ';
Valuearray1 [12]: = '000000'; valuearray1 [13]: = '000000'; valuearray1 [14]: = '000000 ';
Valuearray1 [15]: = '20140901 ';
For I: = 0 to 15 do
If I> = 10 then valuearray2 [I]: = CHR (65 + (I-10 ))
Else valuearray2 [I]: = inttostr (I) [1];
Result: = '';
If action then
Begin // convert a binary string to a hexadecimal string
If (length (valuea) mod 4 <> 0) then
Valuea: = stringofchar ('0', length (valuea) mod 4) + valuea;
While (length (valuea)> = 4) do
Begin
For I: = 0 to 15 do
If copy (valuea, 1, 4) = valuearray1 [I] Then
Result: = Result + valuearray2 [I];
Valuea: = copy (valuea, 5, length (valuea)-4 );
End;
End
Else begin // convert a hexadecimal string to a binary string
While (length (valuea)> = 1) do
Begin
For I: = 0 to 15 do
If copy (valuea, 1, 1) = valuearray2 [I] Then
Result: = Result + valuearray1 [I];
Valuea: = copy (valuea, 2, length (valuea)-1 );
End;
End;
End;
// Convert hexadecimal characters to Integers
Function hexchartoint (hextoken: ansichar): integer;
Begin
Result: = 0;
If (hextoken> #47) and (hextoken <#58) Then {chars 0... 9}
Result: = ord (hextoken)-48
Else
If (hextoken> #64) and (hextoken <#71) Then {chars a... f}
Result: = ord (hextoken)-65 + 10;
End;