如,我的string为CA 01 A0 A1,我要把其转换为byte数组然后用串口发出,如何能让设备收到的也是CA 01 A0 A1,而不是一串数字?
解决方案 »
- TdxNavBar 如何用?
- 请问.duc文件怎么打开,一直打不开郁闷死了(第一次用delphi开发)
- PKG概念问题,不懂,高分求教
- delphi中快速實現數據庫樹形結構并實現treeview導航數據
- 高手能否给出用 Delphi 编出 像Windows 下的 记事本 的主要代码?
- 求助:如何自己开发一个pdf文档阅读器?
- 要学Delphi从什么地方入手啊?
- 我想在程序里加入播放mp3的功能,请问用哪个控件比较好(不是mediaplayer)
- 如何取表中的最后一条纪录?
- 究竟是应该建立一个普通程序去实现还是应该建立一个服务程序去实现呢?高手请进.
- 保存PDF文件到服务器
- 如何把十六进制字符串,以字节为单位,转换成十进制字符串?
bs:array of byte;
bs:=setlenght(bs,lenght(s));
for i:=1 to lenght(s) do
bs[i-1]:=Ord(s[i]);...接收方用Chr(bs[j])ASCII码,没概念?
bs[0]:=Ord('A');
这个报错吗?
哈哈....这个语法都能不过啊
bs:array[0..1023] of byte;
begin
fillchar(bs, sizeof(bs), #0);
//这里假设你的S长度不大于1K
move(PChar(s)^, bs, length(s));
//下面就是你的发送代码了
....
end;
过了,是我自己多加了个填充零的语句
不过我用串口精灵看了一下,我发出的数据还都为数字,是十进制的数字
有没有办法就把我的string原样不动的转为array of byte
ASCII就是数字,你找个ASCII码表看看
‘0’-48,
……
‘9’-57,
....
'A' - 65,'a' - 97
Chr(48) - '0'Chr(65) - 'A'Chr(97) - 'a'
对方我想应该收的是4个字节吧,每个字节用16进制表示是CA 01 A0 A1.
至于是16进制显示,还是10进制显示,这是串口精灵软件的表示给你看方式,在内存中都是0和1。
如果你愿意你还可以用8进制来看。
我就是想要,如果我的STRING中是CA,那就给我转到BYTE后也是CA,或都数字202,可是我CA转过去后,变成ASCII码表上标的数字了
s2:='CA';
s2:='$'+s2;
aByte:=inttostr(s2);哈哈,要说清楚啊
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;
就是上面的代码了! 你测试一下看看是不是你要的!
var
iLen: Integer;
begin
Result := 0;
iLen := length(HesStr);
if iLen <> 2 then Exit;
If not (HesStr[1] in ['0'..'9', 'A'..'F', 'a'..'f']) Then Exit;
If not (HesStr[2] in ['0'..'9', 'A'..'F', 'a'..'f']) Then Exit;
Result := StrToInt('$' + HesStr);
end;
上面这个函数更简洁一些!
感谢楼主。简而言之: Byte_Type_Var:=StrToInt('$' +String_Type_Var); //Length(String_Type_Var)<=2;