Delphi to convert 10 to 16 into the function of the conversion system

Source: Internet
Author: User
Tags chr

Delphi has a direct conversion of 10 into the 16-system function:

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 in to 2,8,16 system
function Inttobitstr (intstr:string): string;
function Inttohexstr (intstr:string): STRING;//10 = 2
function Inttoostr (intstr:string): string;

2 in to 10,8,16 system
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 system
function Hextointstr (hexstr:string): String;
function Hextobitstr (hexstr:string): string;
function Hextoostr (hexstr:string): string;

octal to binary strings
function Otobitstr (o:string): string;
function Otointstr (o:string): string;
function Otohexstr (o:string): string;
End
Var
Tc:tconversion;


Implementation

{Tconversion}
2 to 10 in-system

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
Binary conversion to decimal start
{
Var
str:string;
Int:integer;
I:integer;

STR: = uppercase (Edit1.text);
Int: = 0;
For I: = 1 to Length (str) do
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) Secondary 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

Class
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
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
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

Class
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
0..9:vhextstr[vlen]: = CHR (VD + $);
End
Dec (Vlen);
I: = 0;
VD: = 0;
End
End

If I > 0then
Begin
Case VD
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) do
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 with format
Result:=format ('%d ' +result,[i mod 2]);
I:=i Div 2
End
End
The second method of 10 into 2 system
{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) do
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.

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.