如何把十六进制字符串,以字节为单位,转换成十进制字符串?

wuwenzhe 2012-11-05 04:56:01
如何把这串字符"32463542313930302D314346452D303830312D41",以字节为单位,转成十进制数呢?
有否现成的函数?
...全文
779 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhz8000 2013-06-07
  • 打赏
  • 举报
回复
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; 就是上面的代码了! 你测试一下看看是不是你要的!
xhz8000 2012-11-12
  • 打赏
  • 举报
回复
这个不知道是不是你要的结果,有问题告诉我! 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;
广州佬 2012-11-08
  • 打赏
  • 举报
回复
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;
SQLDebug_Fan 2012-11-06
  • 打赏
  • 举报
回复
这个是把内存DUMP出来,以16进制显示,你只要把这些再写入就可以了,DELPHI有这个函数: HexToData,然后调用IntToStr、FloatToStr就可以显示值了。
wuwenzhe 2012-11-06
  • 打赏
  • 举报
回复
二楼那段代码什么意思?能否解释一下?
wuwenzhe 2012-11-06
  • 打赏
  • 举报
回复
引用 1 楼 gzzai 的回复:
Delphi/Pascal code 12345678910 procedure TForm1.Button1Click(Sender: TObject); const hexChars='0123456789ABCDEF'; var s:string; i:integer; begin s:='32463542313930302D314346452D3038303……
pos()的作用是什么?
Jekhn 2012-11-06
  • 打赏
  • 举报
回复
以字节为单位是什么意思?
广州佬 2012-11-06
  • 打赏
  • 举报
回复

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;
广州佬 2012-11-06
  • 打赏
  • 举报
回复
看看pos函数的原型嘛,pos(‘子串’,‘字符串’) 将返回“子串”在“字符串”的起始位置。
BambooCaep 2012-11-06
  • 打赏
  • 举报
回复
hextobin,inttostr
广州佬 2012-11-05
  • 打赏
  • 举报
回复
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;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧