IMTP 发表于 2009-9-6 17:21:36

DELPHI中如何用 TEdit 发送 hex 码?

比如我在 EDIT 中 输入0A, 如何发送 0X0A 出去呢? 输入的当是字符串吧。
还有 输入 1234 ,怎样才能得到 0x12,0x34,两个字节呢?

_yun_yun_ 发表于 2009-9-6 17:25:28

strtohex()

IMTP 发表于 2009-9-6 17:36:58

好像没有 StrToHex() 这个函数吧。只有 IntToHex()
我把 字符串转为整数 strtoint() 在转为HEXIntToHex(); 但是编译错误。
[错误] Unit1.pas(193): There is no overloaded version of 'IntToHex' that can be called with these arguments
[致命错误] B8303.dpr(5): Could not compile used unit 'Unit1.pas'

ShangGuan 发表于 2009-9-6 20:01:06

C/C++ 有strtol,strtod,atoi,atol

IMTP 发表于 2009-9-6 20:15:51

可是在是在用DELPHI

mimawangle 发表于 2009-9-6 20:56:10

strtoint就可以了,其中abcdef前自己加$后再转换

SkyGz 发表于 2009-9-7 14:00:17

按你要求弄的
Function StrToHexStr(Const S: String): String;
//字符串转换成16进制字符串
Var
I: Integer;
Begin
For I := 1 To Length(S) Do
Begin
    If I = 1 Then
      Result := IntToHex(Ord(S), 2)
    Else Result := Result + '0x' + IntToHex(Ord(S), 2) + ',';
End;
delete(result,length(result)-1,1);
End;

rkfch 发表于 2009-9-10 10:01:52

这个是Delphi上的例子,可以用作参考:
The following example uses an edit control, a button, and a label on a form. When the button is clicked, the hexadecimal value of each character in the edit control is displayed in the label.

procedure TForm1.Button1Click(Sender: TObject);

var
i: Integer;
begin
Label1.Caption := '';
for i := 1 to Length(Edit1.Text) do
begin
    try
      Label1.Caption := Label1.Caption + IntToHex(Edit1.Text,2) + ' ';
    except
      Beep;
    end;
end;
end;
页: [1]
查看完整版本: DELPHI中如何用 TEdit 发送 hex 码?