1.16 to 10
********************************************************
16 ext. 10, otherwise output-1
function Hex (C:char): Integer;
Var
X:integer;
Begin
if (ord (c) >= Ord (' 0 ')) and (Ord (c) <= Ord (' 9 ')) then
X:= Ord (c)-ord (' 0 ')
else if (Ord (c) >= Ord (' a ')) and (Ord (c) <= ord (' F ')) then
X:= Ord (c)-ord (' a ') + 10
else if (Ord (c) >= ord (' A ')) and (Ord (c) <= ord (' F ')) then
X:= Ord (c)-ord (' A ') + 10
Else
x:=-1; Input error
Result:= x;
End
2. Character to 10
**********************************************
The function receives one or two, the character conversion succeeds after the output corresponds to the value of 10 binary number, otherwise output -1
function HexToInt (str:string): Integer;
Var
TmpInt1, Tmpint2:integer;
Begin
If Length (Str) = 1 Then
Begin
Result:= Hex (str[1]);
End
else if Length (Str) = 2 Then
Begin
Tmpint1:= Hex (str[1]);
Tmpint2:= Hex (str[2]);
if (tmpInt1 =-1) or (TmpInt2 =-1) Then
result:=-1
Else
result:= tmpInt1 * + tmpInt2;
End
Else
result:=-1; Input error, conversion failed
End
3. String to ASCII code
*******************************************************************
String converted to ASCII code string
function strtohexstr (const s:string): string;
Var
I:integer;
Begin
For i:= 1 to Length (S) do
Begin
If i = 1 Then
result:= Inttohex (Ord (S[1]), 2)
Else
result:= Result + ' + inttohex (Ord (S[i]), 2);
End
End
4. String to remove all spaces
***********************************************************************
This function removes all spaces in the string
function Trimall (str:string): string;
Var
Mlen, I:integer;
Begin
mlen:= Length (STR);
Initialize return value
Result:= ";
For i:= 0 to Mlen do
Begin
It's a space.
If str[i] = "Then
Continue;
result:= Result + str[i];
End
End
5.10 to 2
**************************************************
Convert decimal to binary string
function Dtob (decimal:longint): string;
Const symbols:string[16]= ' 01 ';
Var
scratch:string;
Remainder:byte;
Begin
Repeat
Remainder:=decimal MoD 2;
Scratch:=symbols[remainder+1]+scratch;
Decimal:=decimal Div 2;
Until (decimal=0);
Result:=scratch;
End
6.10 TO16 Characters
**************************************************
1. Convert decimal to hexadecimal string
function Dtohex (decimal:longint): string;
Const symbols:string[16]= ' 0123456789abcdef ';
Var
scratch:string;
Remainder:byte;
Begin
Repeat
Remainder:=decimal MoD 16;
Scratch:=symbols[remainder+1]+scratch;
Decimal:=decimal Div 16;
Until (decimal=0);
Result:=scratch;
End
2. Decimal to hexadecimal characters
var hexarr:array[1..15]of string= (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ');
function Int2hex (Value:integer): string;
Var
Itemp:integer;
I:integer;
Begin
Result: = ';
I: = 0;
While i<4 do
Begin
case I of
0:itemp: = Value shr and $FF;
1:itemp: = Value shr + $FF;
2:itemp: = Value shr 8 and $FF;
3:itemp: = Value and $FF;
End
If not Boolean (iTemp) then result: = result + ' 00 '
Else
Begin
Result: = result + hexarr[itemp Div 16];
Result: = result + hexarr[itemp mod 16];
End
INC (i);
End
End
7. String to 16 binary
*************************************************************************
Eidt1.text:=strtoint (' $ ' + ' 0000FFFF '); 65535
2>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>
Unit Unit2;
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 > 0then
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 > 0then
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..15] 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 do 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 and format it using format
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 do 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.
Delphi--Binary conversion function table