Home > Back-end >  Questions about hexadecimal conversion
Questions about hexadecimal conversion

Time:09-26

2011 can use inttohex convert hexadecimal is $7 $DB
This is the high byte before, and the low byte in the

What to do to get the $7 $DB into 2011?

thank you

CodePudding user response:

I:=StrToInt (' $' + '07 db);



CodePudding user response:

Or your custom function, convert hexadecimal number to a decimal number
 
The function HexToInt (Hexa: string) : LongWord;
Const
ValoresHexa: array (' A '... 'F') of integer=(10, 11, 12, 13, 14, 15);
Var
NDecimal: LongWord;
NIndex: byte;
The begin
NDecimal:=0;
Hexa:=Uppercase (Hexa);
For nIndex:=Length (Hexa) downto 1 do
If Hexa [nIndex] in [' 0 '... '9']
Then nDecimal:=nDecimal + StrToInt (Hexa [nIndex]) *
Trunc (Exp ((Length (Hexa) - nIndex) * ln (16)))
The else nDecimal:=nDecimal + ValoresHexa [Hexa [nIndex]] *
Trunc (Exp ((Length (Hexa) - nIndex) * ln (16)));
Result:=nDecimal;
end;

CodePudding user response:

Give several functions you consult, see can according to your need to change,
 
Type
TarrayByte=an array of Byte;

Procedure HexStrToArray (const Str: string;
Var aData: TarrayByte; Const DataBegin DataLength: Integer);
Var
I: Integer;
The begin
If Length (Str)=2 * DataLength then
The begin
For I:=0 to do DataLength - 1
The begin
AData [DataBegin + I] :=StrToInt (' $' + Str [2 * I + 1) + Str (2 * I + 2]).
end;
end;
end;

The function HexStrToByteArray (const S: string) : TarrayByte;
//hexadecimal string into a Byte array
Var
T, I: Integer;
Ts: the string;
M, Code: Integer;
The begin
T:=1;
I:=0;
SetLength (Result, Length (S));
While t<=Length (S) do
The begin
While not [t] in HexBytes (S) do
Inc (t);
If (t + 1 & gt; Length (S)) or (not (S/t + 1 in HexBytes)) then
Ts:='$' + S [t]
The else
Ts:='$' [t] + S + S (t + 1),
Val (ts, M Code);
If Code=0 then
The begin
Result: [I]=M;
Inc (I);
end;
Inc (t, 2);
end;
SetLength (the Result, I);
end;

The function HexStrToStr (const S: string) : string;
//hexadecimal string into a string
Var
T: Integer;
K: Integer;
Ts: the string;
M, Code: Integer;
The begin
T:=1;
SetLength (Result, (Length (S) + 1) SHR 1);
K:=1;
While t<=Length (S) do
The begin
While not [t] in HexBytes (S) do
Inc (t);
If (t + 1 & gt; Length (S)) or (not (S/t + 1 in HexBytes)) then
Ts:='$' + S [t]
The else
Ts:='$' [t] + S + S (t + 1),
Val (ts, M Code);
If Code=0 then
The begin
Result: [K]=CRH (M);
Inc (K);
end;
Inc (t, 2);
end;
If KSetLength (Result, K - 1);
end;

CodePudding user response:

//Delphi hexadecimal hundreds of $as the prefix, so will be in the form of hexadecimal string into an integer can be: 
The function HexToDec (const AHexString: String) : Integer;
The begin
Result:=StrToInt (' $' + AHexString);
end;

Procedure TForm1. Button1Click (Sender: TObject);
The begin
Edit1. Text:=IntToStr (hexToDec1 (' 07 db));
end;


The result is 2011
  • Related