Delphi Binary Conversion

Source: Internet
Author: User
Tags ord


----binary to 16 binary

function Binarytohex (strbinary:string): string;
Var
Vd:byte;
I:integer;
vhextstr:string;
Vp:pchar;
Vlen:integer;
Begin
VLen: = Length (strbinary);
If VLen mod 4 > 0 Then
Begin
SetLength (Vhextstr, VLen Div 4 + 1);
VLen: = VLen Div 4 + 1;
End
Else
Begin
SetLength (Vhextstr, VLen div 4);
VLen: = VLen Div 4;
End

Initialization
VD: = 0;
VP: = PChar (strbinary) + length (strbinary)-1;
I: = 0; Start Count

While vp^ <> #0 do
Begin
If vp^ = ' 1 ' Then
Begin
case I of
0:
VD: = VD + 1;
1:
VD: = VD + 2;
2:
VD: = VD + 4;
3:
VD: = VD + 8;
End
End

Dec (VP);
INC (i);
If I = 4 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
10.. 15:
Vhextstr[vlen]: = CHR (vD-10 + $41);
End
Dec (VLen);
I: = 0;
VD: = 0;
End
End

If i > 0 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
10.. 15:
Vhextstr[vlen]: = CHR (VD + $41);
End
End

Result: = Vhextstr;
End


---16 binary to Binary

function Hextobinary (hex:string): string;

Const

Box:array [0..] of String =

(' 0000 ', ' 0001 ', ' 0010 ', ' 0011 ',

' 0100 ', ' 0101 ', ' 0110 ', ' 0111 ',

' 1000 ', ' 1001 ', ' 1010 ', ' 1011 ',

' 1100 ', ' 1101 ', ' 1110 ', ' 1111 ');

Var

I:integer;

Begin

For I: = Length (hex) Downto 1 Do

Result: = Box[strtoint (' $ ' + hex[i]) + result;

End


-----hexadecimal to 10 binary

function HexToInt2 (hex:ansistring): integer;
Var
I:integer;
function Ncf (num, f:integer): integer;
Var
I:integer;
Begin
Result: = 1;
If f = 0 Then
Exit
For I: = 1 to F do
Result: = result * NUM;
End
function Hexchartoint (hextoken:ansichar): integer;
Begin
If Ord (Hextoken) >
Hextoken: = Ansichar (Chr (Ord (Hextoken)-32));
Result: = 0;
if (ord (Hextoken) >) and (Ord (Hextoken) <) then {chars 0....9}
Result: = Ord (Hextoken)-48
else if (ord (Hextoken) >) and (Ord (Hextoken) <) then {chars A .... F
Result: = Ord (Hextoken)-55;
End

Var
A16int:integer;
Ancf:integer;
Begin
Result: = 0;
Hex: = ansiuppercase (Trim (hex));
If hex = "Then
Exit
For I: = 1 to Length (hex) does
Result: = result + Hexchartoint (Hex[i]) * NCF (Length (hex)-i);
Begin
A16int: = Hexchartoint (Hex[i]);
ANCF: = NCF (+, Length (hex)-i);
Result: = result + A16int * ANCF;
End
End

--------------------------------------------------------------------------------

Delphi has a function that converts the 10 binary directly into the 16 binary:

function Inttohex (Value:integer; Digits:integer): string;
overload;

function Inttohex (value:int64; Digits:integer): string; overload;

Unit uconversion;

Interface

Uses
Sysutils, Math;

Type
Tconversion = Class
Public
10 binary to 2,8,16 binary
function Inttobitstr (intstr:string): string;
function Inttohexstr (intstr:string): string; 10 = 2
function Inttoostr (intstr:string): string;

2 binary to 10,8,16 binary
function Bittoint (bitstr:string): Longint; 2 = 10
function bitstrtohextstr (const bitstr:string): String; 2 = 16
function bitstrtoostr (const bitstr:string): String; 2 = 8

> 10 2 8 Binary
function Hextointstr (hexstr:string): String;
function Hextobitstr (hexstr:string): string;
function Hextoostr (hexstr:string): string;

octal into binary string
function Otobitstr (o:string): string;
function Otointstr (o:string): string;
function Otohexstr (o:string): string;
End

Var
Tc:tconversion;

Implementation

{Tconversion}
2 binary to 10 binary

function Tconversion.bittoint (bitstr:string): Longint;
Var
I, Size:integer;
Begin
Result: = 0;
Size: = Length (BITSTR);
For I: = Size downto 1 Do
Begin
For example 1010
If Copy (Bitstr, I, 1) = ' 1 ' Then
Result: = result + (1 SHL (size-i));
End
The second method of
Binary conversion to decimal start
{
Var
str:string;
Int:integer;
I:integer;

STR: = uppercase (Edit1.text);
Int: = 0;
For I: = 1 to Length (str) does
int: = int * 2 + ORD (Str[i])-48;
EDIT2.TEXT:=INTTOSTR (int);
}
binary conversion to decimal end;
The third method
{
function Hextoint (s:string): Double;
Begin
While Length (s) <>0 do
Begin//2^ (Length-1) sub-square
If s[1]= ' 1 ' then Result:=result+power (2,length (s)-1);
S:=copy (S,2,length (s));
End
End
}
End

function tconversion.bitstrtohextstr (const bitstr:string): String;
Var
Vd:byte;
I:integer;
vhextstr:string;
Vp:pchar;
Vlen:integer;
Begin
VLen: = Length (BITSTR);
If VLen mod 4 > 0 Then
Begin
SetLength (Vhextstr, VLen Div 4 + 1);
VLen: = VLen Div 4 + 1;
End
Else
Begin
SetLength (Vhextstr, VLen div 4);
VLen: = VLen Div 4;
End

Initialization
VD: = 0;
VP: = PChar (BITSTR) + Length (BITSTR)-1;
I: = 0; Start Count

While vp^ <> #0 do
Begin
If vp^ = ' 1 ' Then
Begin
case I of
0:
VD: = VD + 1;
1:
VD: = VD + 2;
2:
VD: = VD + 4;
3:
VD: = VD + 8;
End
End

Dec (VP);
INC (i);
If I = 4 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
10.. 15:
Vhextstr[vlen]: = CHR (vD-10 + $41);
End
Dec (VLen);
I: = 0;
VD: = 0;
End
End

If i > 0 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
10.. 15:
Vhextstr[vlen]: = CHR (VD + $41);
End
End

Result: = Vhextstr;
End

function tconversion.bitstrtoostr (const bitstr:string): String;
Var
Vd:byte;
I:integer;
vhextstr:string;
Vp:pchar;
Vlen:integer;
Begin
VLen: = Length (BITSTR);
If VLen mod 3 > 0 Then
Begin
SetLength (Vhextstr, VLen Div 3 + 1);
VLen: = VLen Div 3 + 1;
End
Else
Begin
SetLength (Vhextstr, VLen Div 3);
VLen: = VLen Div 3;
End

Initialization
VD: = 0;
VP: = PChar (BITSTR) + Length (BITSTR)-1;
I: = 0; Start Count

While vp^ <> #0 do
Begin
If vp^ = ' 1 ' Then
Begin
case I of
0:
VD: = VD + 1;
1:
VD: = VD + 2;
2:
VD: = VD + 4;
End
End

Dec (VP);
INC (i);
If I = 3 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
End
Dec (VLen);
I: = 0;
VD: = 0;
End
End

If i > 0 Then
Begin
Case VD of
0.. 9:
Vhextstr[vlen]: = CHR (VD + $);
End
End

Result: = Vhextstr;
End

function Tconversion.hextobitstr (hexstr:string): string;
Const
Cbitstrings:array [0] of string = (' 0000 ', ' 0001 ', ' 0010 ', ' 0011 ',
' 0100 ', ' 0101 ', ' 0110 ', ' 0111 ', ' 1000 ', ' 1001 ', ' 1010 ', ' 1011 ', ' 1100 ',
' 1101 ', ' 1110 ', ' 1111 ');
Var
I:integer;
Begin
Result: = ';
For I: = 1 to Length (HEXSTR) does
Result: = result + cbitstrings[strtointdef (' $ ' + hexstr[i], 0)];
While Pos (' 0 ', Result) = 1 does
Delete (Result, 1, 1);
End {Hextobit}

function Tconversion.hextointstr (hexstr:string): String;
Begin
Result: = IntToStr (Strtoint (' $ ' + (HEXSTR)));
End

function Tconversion.hextoostr (hexstr:string): string;
Begin
Result: = Bitstrtoostr (Hextobitstr (HEXSTR));
End

function Tconversion.inttobitstr (intstr:string): string;
Var
I:integer;
Begin
I: = Strtoint (INTSTR);
While I <> 0 do
Begin//I mod 2 modulo, then format formatted
Result: = Format ('%d ' + Result, [i mod 2]);
I: = i div 2
End
End
10 binary Swap 2 binary second method
{function Tconversion.inttobitstr (Value, Size:integer): String;
Var
I:integer;
Begin
Result:= ";
For i:=size-1 Downto 0 DO begin
If Value and (1 SHL i) <>0 then BEGIN
result:=result+ ' 1 ';
End ELSE begin
result:=result+ ' 0 ';
End
End
End }

function Tconversion.inttohexstr (intstr:string): string;
Begin
Result: = Inttobitstr (INTSTR);
End

function Tconversion.inttoostr (intstr:string): string;
Begin
Result: = Bitstrtohextstr (Inttobitstr (INTSTR));
End

function Tconversion.otobitstr (o:string): string;
Const
Cbitstrings:array [0.. 7] of String = (' 000 ', ' 001 ', ' 010 ', ' 011 ', ' 100 ',
' 101 ', ' 110 ', ' 111 ');
Var
I, J:integer;
Begin
Result: = ';
For I: = 1 to Length (O) does
Begin
J: = Strtoint (O[i]);
Result: = result + Cbitstrings[j];
End
While Pos (' 0 ', Result) = 1 does
Delete (Result, 1, 1);
End

function Tconversion.otohexstr (o:string): string;
Begin
Result: = Bitstrtohextstr (Otobitstr (O));
End

function Tconversion.otointstr (o:string): string;
Begin
Result: = Otointstr (Otobitstr (O));
End

End.

This article is from the "All for the law, such as Fleeting Dream" blog, please be sure to keep this source http://kaixinbuliao.blog.51cto.com/2567365/1937471

Delphi Binary Conversion

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.