Conversion between strings and binary numbers __integer

Source: Internet
Author: User
Tags ord

A function that converts a string (which can contain Chinese characters) to a binary number: Convertstrtobin (); a function to convert the binary number to a string: Convertbintostr ().
The following two functions can also be processed for strings that contain Chinese characters, and may be converted to Mandarin when reversed.
Function Convertstrtobin (value:string): string;//converts strings to binary numbers
var temphex:string[2];
I:integer;
Begin
Result: = ';
If trim (Value) = ' then Exit;
Temphex: = ';
For I: = 1 to Length (Value) does
Begin
Temphex: = Inttohex (Ord (Value[i)), 2)//char to two-digit hexadecimal number
Result: = result + Bintohexeachother (temphex,false);//hexadecimal to binary
End
End

Function convertbintostr (value:string): string; Converts binary data to strings
Var temphex:string;
I, Tempint:integer;
Begin
Result: = ';
If trim (Value) = ' then Exit;
Temphex: = Bintohexeachother (value,true);//binary turns to hexadecimal
I: = 0;
Repeat
Begin
I: = i + 1;
Tempint: = Hexchartoint (Temphex[i]);
I: = i + 1;
Tempint: = Tempint * + hexchartoint (temphex[i]);
This converts the two-bit hexadecimal number to a decimal number
Result: = result + Chr (tempint); Turn into ASCII code
End
Until i >= length (Temphex)
End

The functions to be called in the two inverse functions hexchartoint () and Bintohexeachother () are as follows:

function Bintohexeachother (valuea:string; Action:boolean): string;
Converts a binary string to a hexadecimal string or vice versa
Var
Valuearray1:array [0..15] of string[4];
Valuearray2:array [0..15] of char;
I:shortint;
Begin
Array initialization
Valuearray1[0]: = ' 0000 ';  VALUEARRAY1[1]: = ' 0001 '; VALUEARRAY1[2]: = ' 0010 ';
VALUEARRAY1[3]: = ' 0011 ';  VALUEARRAY1[4]: = ' 0100 '; VALUEARRAY1[5]: = ' 0101 ';
VALUEARRAY1[6]: = ' 0110 ';  VALUEARRAY1[7]: = ' 0111 '; VALUEARRAY1[8]: = ' 1000 ';
VALUEARRAY1[9]: = ' 1001 ';  VALUEARRAY1[10]: = ' 1010 '; VALUEARRAY1[11]: = ' 1011 ';
VALUEARRAY1[12]: = ' 1100 ';  VALUEARRAY1[13]: = ' 1101 '; VALUEARRAY1[14]: = ' 1110 ';
VALUEARRAY1[15]: = ' 1111 ';
For I: = 0 todo
If I >= then valuearray2[i]: = Chr (+ (I-10))
else Valuearray2[i]: = IntToStr (i) [1];

Result: = ';
If Action Then
Begin//binary string converted to 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 todo
If Copy (valuea,1,4) = Valuearray1[i] Then
Result: = result + Valuearray2[i];
Valuea: = Copy (Valuea,5,length (Valuea)-4);
End
End
else begin//16 binary string converted into binary strings
while (Length (Valuea) >= 1) do
Begin
For I: = 0 todo
If Copy (valuea,1,1) = Valuearray2[i] Then
Result: = result + Valuearray1[i];
Valuea: = Copy (Valuea,2,length (Valuea)-1);
End
End
End

function Hexchartoint (Hextoken:char): 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

Processing effect as shown in the figure
///////////////////////////////////////////////////////////////////////////////////

Learn a trick, this is really more convenient ....
Procedure Tform1.bitbtn1click (Sender:tobject);
var Myint:integer;
Begin
Myint: = Strtoint (' $ ' + ' 3 a '); Myint = 58
ShowMessage (IntToStr (Myint));
End

/////////////////////////////////////////////////////////////////////////////////////////////////////////

A file with a suffix named *.bin needs to be read in binary form (needed to be shown in the Memo box)
It can then be modified and saved as a bin file.
==================================================================
This is the code that reads. bin and displays it in the TMemo box:
Among them, opendialog1.options: = [ofhidereadonly,ofpathmustexist,offilemustexist,ofenablesizing];
Opendialog1.filter: = ' *.bin|*.bin ';
Opendialog1.defaultext: = ' *.bin ';

Procedure Tform1.button1click (Sender:tobject);
Var
F:file of Byte;
Fs,i:integer;
Bytes:array of Byte;
tempstr:string;
Begin
If Opendialog1.execute Then
Begin
TempStr: = ';
AssignFile (F,opendialog1.filename);
Reset (f);
Fs:=filesize (f);//The number of bytes returned is correct
SetLength (BYTES,FS);
I: = 0;
Repeat
Seek (F,i);
Read (F,bytes[i]);
TempStr: = tempstr + ' + inttohex (bytes[i],2);
INC (i);
Until I >= FS;
TempStr: = Copy (Tempstr,2,length (TEMPSTR));
Memo1.text: = TempStr;
CloseFile (f);
End
End

==================================================================
This is the code that writes the binary file displayed in the TMemo box to the. bin file of the specified file name:
wherein savedialog1.options: = [ofoverwriteprompt,ofhidereadonly,ofpathmustexist,ofenablesizing];
Savedialog1.filter: = ' *.bin|*.bin ';
Savedialog1.defaultext: = ' *.bin ';

Procedure Tform1.button2click (Sender:tobject);
Var
F:tfilestream;
data:string;
Buf:pchar;
Begin
Data: = Memo1.text;
Data: = StringReplace (data, ', ', ', [Rfreplaceall]);//Remove spaces
If Savedialog1.execute Then
Begin
Getmem (buf,length (data) Div 2);
Hextobin (@data [1],buf,length (data) Div 2); hexadecimal string converted to binary
F: = Tfilestream.create (Savedialog1.filename, fmcreate);
Try
F.seek (0, sofrombeginning);
F.write (buf^, Length (data) Div 2);
Finally
F.free;
End
Freemem (BUF);
End
End

And then by reading and writing. bin file, put the saved. Bin back to the TMemo box, found that the display results are the same as the original file, if you need to modify the file to be saved, you can modify the row in the TMemo box to save, the result of the display is correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.