2011可以用inttohex轉換成16進制是 $07 $DB
這個是高位元組在前,低位元組在后
怎么做,才能把 $07 $DB 轉換成2011呢?
謝謝。
uj5u.com熱心網友回復:
I := StrToInt('$' + '07DB');uj5u.com熱心網友回復:
或者你自定義函式,轉換十六進制數為十進制數
function HexToInt(Hexa: string): LongWord;
const
ValoresHexa: array['A'..'F'] of integer = (10, 11, 12, 13, 14, 15);
var
nDecimal: LongWord;
nIndex: byte;
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)))
else nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
Trunc(Exp((Length(Hexa) - nIndex) * ln(16)));
Result := nDecimal;
end;
uj5u.com熱心網友回復:
給幾個函式你參考一下,明白了就可以根據自己需要改了。
type
TarrayByte = array of Byte;
procedure HexStrToArray(const Str: string;
var aData: TarrayByte; const DataBegin, DataLength: Integer);
var
I:Integer;
begin
if Length(Str)=2*DataLength then
begin
for I:=0 to DataLength-1 do
begin
aData[DataBegin+I]:=StrToInt('$'+Str[2*I+1]+Str[2*I+2]);
end;
end;
end;
function HexStrToByteArray(const S: string): TarrayByte;
//16進制字串轉換成Byte陣列
var
t,I:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
I:=0;
SetLength(Result,Length(S));
while t<=Length(S) do
begin
while not (S[t] in HexBytes) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in HexBytes)) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
begin
Result[I]:=M;
Inc(I);
end;
inc(t,2);
end;
SetLength(Result,I);
end;
function HexStrToStr(const S: string): string;
//16進制字串轉換成字串
var
t:Integer;
K:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
SetLength(Result,(Length(S)+1) shr 1);
K:=1;
while t<=Length(S) do
begin
while not (S[t] in HexBytes) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in HexBytes)) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
begin
Result[K]:=Chr(M);
Inc(K);
end;
inc(t,2);
end;
if K<Length(Result)+1 then
SetLength(Result,K-1);
end;
uj5u.com熱心網友回復:
// Delphi 中 16 進制數以 $ 為前綴,所以將 16 進制形式的字串轉換為整數即可:
function HexToDec(const AHexString: String): Integer;
begin
Result := StrToInt('$' + AHexString);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=IntToStr(hexToDec1('07DB'));
end;
結果就是2011
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/81132.html
標籤:語言基礎/算法/系統設計
