procedure TForm1.Button1Click(Sender: TObject); const hexChars='0123456789ABCDEF'; var s:string; i:integer; begin s:='32463542313930302D314346452D303830312D41'; for i:=1 to length(s) do showmessage(inttostr(pos(s[i],hexChars)-1)); end;
procedure TForm1.Button1Click(Sender: TObject); const hexChars='0123456789ABCDEF'; //定义一个字符串常量(这里还注释,好像不太礼貌) var s:string; i:integer; begin s:='32463542313930302D314346452D303830312D41'; for i:=1 to length(s) do//循环取s中的每个字符 showmessage(inttostr(pos(s[i],hexChars)-1));//将取出的字符作“子串”,在上面定义的常量中找匹配的位置,从而得出对十进制的映射。 end;
procedure TForm1.Button2Click(Sender: TObject); const hexChars='0123456789ABCDEF'; datastr='32 46 35 42 31 39 30 30 2D 31 43 46 45 2D 30 38 30 31 2D 41'; var s,tmp1,tmp2:string; x:integer; function Getbyte(hexstr:string):integer; begin Result:=-1; if length(hexstr)<>2 then exit; Result:=(pos(hexstr[1],hexChars)-1) shl 4 + pos(hexstr[2],hexChars)-1; end; begin s:=datastr; tmp1:=''; while length(s)>0 do begin x:=Getbyte(s[1]+s[2]); tmp2:=tmp2+inttostr(x)+','; tmp1:=tmp1+char(x); delete(s,1,3); end; showmessage(datastr+#13+tmp2+' ___ '+tmp1); end;
这个不知道是不是你要的结果,有问题告诉我!procedure TForm1.Button1Click(Sender: TObject); var vTempStr: String; vTempByte: Char; byte1, byte2: Byte; iCount, iStart, bResult: Integer;begin Memo1.Lines.Clear; vTempStr := '32463542313930302D314346452D303830312D41'; iCount := length(vTempStr); iStart := 1; while iStart < iCount do begin vTempByte := vTempStr[iStart]; byte1 := HexToTen(vTempByte); iStart := iStart + 1; vTempByte := vTempStr[iStart]; byte2 := HexToTen(vTempByte); //byte1 ÔÚ¸ß룬byte2 ÔÚµÍλ bResult := TwoHexToByte(byte1, byte2); Memo1.Lines.Add(IntToHex(bResult, 2)); iStart := iStart + 1; end; end;function TForm1.HexToTen(AHexStr: Char): Byte; begin AHexStr := UpCase(AHexStr); case AHexStr of '0': Result := 0; '1': Result := 1; '2': Result := 2; '3': Result := 3; '4': Result := 4; '5': Result := 5; '6': Result := 6; '7': Result := 7; '8': Result := 8; '9': Result := 9; 'A': Result := 10; 'B': Result := 11; 'C': Result := 12; 'D': Result := 13; 'E': Result := 14; 'F': Result := 15; else Result := 0; end; end;function TForm1.TwoHexToByte(Hex1, Hex2: Byte): Byte; begin Result := (Hex1 shl 4) or Hex2; end;
function TForm1.HexStrToByte(HesStr: String): Byte; var iLen: Integer; begin Result := 0; iLen := length(HesStr); if iLen <> 2 then Exit; If not (HesStr[1] in ['0'..'9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f']) Then Exit; If not (HesStr[2] in ['0'..'9', 'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f']) Then Exit; Result := StrToInt('$' + HesStr); end; procedure TForm1.Button1Click(Sender: TObject); const SHEX = 'CA01A0A1'; var iByte: Byte; sTemp: String; hexBuf: array[0..1023] of byte; iStart, iCount, idex: Integer; begin idex := 0; iStart := 1; fillchar(hexBuf, sizeOf(hexBuf), #0); iCount := length(SHEX) + 1; while iStart < iCount do begin sTemp := SHEX[iStart]; iStart := iStart + 1; if iStart >= iCount then Break; sTemp := sTemp + SHEX[iStart]; iByte := HexStrToByte(sTemp); ShowMessage(IntToHex(iByte, 2)); hexBuf[idex] := iByte; idex := idex + 1; iStart := iStart + 1; end; end; 就是上面的代码了! 你测试一下看看是不是你要的!
const
hexChars='0123456789ABCDEF';
var s:string;
i:integer;
begin
s:='32463542313930302D314346452D303830312D41';
for i:=1 to length(s) do
showmessage(inttostr(pos(s[i],hexChars)-1));
end;
HexToData,然后调用IntToStr、FloatToStr就可以显示值了。
procedure TForm1.Button1Click(Sender: TObject);
const hexChars='0123456789ABCDEF'; //定义一个字符串常量(这里还注释,好像不太礼貌)
var s:string;
i:integer;
begin
s:='32463542313930302D314346452D303830312D41';
for i:=1 to length(s) do//循环取s中的每个字符
showmessage(inttostr(pos(s[i],hexChars)-1));//将取出的字符作“子串”,在上面定义的常量中找匹配的位置,从而得出对十进制的映射。
end;
const hexChars='0123456789ABCDEF';
datastr='32 46 35 42 31 39 30 30 2D 31 43 46 45 2D 30 38 30 31 2D 41';
var s,tmp1,tmp2:string;
x:integer;
function Getbyte(hexstr:string):integer;
begin
Result:=-1;
if length(hexstr)<>2 then exit;
Result:=(pos(hexstr[1],hexChars)-1) shl 4 + pos(hexstr[2],hexChars)-1;
end;
begin
s:=datastr;
tmp1:='';
while length(s)>0 do begin
x:=Getbyte(s[1]+s[2]);
tmp2:=tmp2+inttostr(x)+',';
tmp1:=tmp1+char(x);
delete(s,1,3);
end;
showmessage(datastr+#13+tmp2+' ___ '+tmp1);
end;
var
vTempStr: String;
vTempByte: Char;
byte1, byte2: Byte;
iCount, iStart, bResult: Integer;begin
Memo1.Lines.Clear;
vTempStr := '32463542313930302D314346452D303830312D41';
iCount := length(vTempStr);
iStart := 1;
while iStart < iCount do
begin
vTempByte := vTempStr[iStart];
byte1 := HexToTen(vTempByte);
iStart := iStart + 1;
vTempByte := vTempStr[iStart];
byte2 := HexToTen(vTempByte);
//byte1 ÔÚ¸ß룬byte2 ÔÚµÍλ
bResult := TwoHexToByte(byte1, byte2);
Memo1.Lines.Add(IntToHex(bResult, 2));
iStart := iStart + 1;
end;
end;function TForm1.HexToTen(AHexStr: Char): Byte;
begin
AHexStr := UpCase(AHexStr);
case AHexStr of
'0': Result := 0;
'1': Result := 1;
'2': Result := 2;
'3': Result := 3;
'4': Result := 4;
'5': Result := 5;
'6': Result := 6;
'7': Result := 7;
'8': Result := 8;
'9': Result := 9;
'A': Result := 10;
'B': Result := 11;
'C': Result := 12;
'D': Result := 13;
'E': Result := 14;
'F': Result := 15;
else
Result := 0;
end;
end;function TForm1.TwoHexToByte(Hex1, Hex2: Byte): Byte;
begin
Result := (Hex1 shl 4) or Hex2;
end;
var
iLen: Integer;
begin
Result := 0;
iLen := length(HesStr);
if iLen <> 2 then Exit;
If not (HesStr[1] in ['0'..'9', 'A', 'B', 'C', 'D', 'E', 'F',
'a', 'b', 'c', 'd', 'e', 'f']) Then Exit;
If not (HesStr[2] in ['0'..'9', 'A', 'B', 'C', 'D', 'E', 'F',
'a', 'b', 'c', 'd', 'e', 'f']) Then Exit;
Result := StrToInt('$' + HesStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
const
SHEX = 'CA01A0A1';
var
iByte: Byte;
sTemp: String;
hexBuf: array[0..1023] of byte;
iStart, iCount, idex: Integer;
begin
idex := 0;
iStart := 1;
fillchar(hexBuf, sizeOf(hexBuf), #0);
iCount := length(SHEX) + 1;
while iStart < iCount do
begin
sTemp := SHEX[iStart];
iStart := iStart + 1;
if iStart >= iCount then Break;
sTemp := sTemp + SHEX[iStart];
iByte := HexStrToByte(sTemp);
ShowMessage(IntToHex(iByte, 2));
hexBuf[idex] := iByte;
idex := idex + 1;
iStart := iStart + 1;
end;
end;
就是上面的代码了! 你测试一下看看是不是你要的!