C++将 StringToByteArray 转换为 Delphi

发布于 2025-01-20 14:42:36 字数 1940 浏览 0 评论 0原文

我试图使用 globalplatform库来自delphi的Karsten Ohme(Kaoh)。在stackoverflow上的某些人的帮助下,我使它在正常工作中工作,并且能够与CardReader建立连接。现在,我正在尝试选择一种援助,因此我需要将援助作为字节数组将其传递给功能。

我使用gpshell(使用同一库的命令行工具)源代码作为参考,以帮助我翻译功能。在那里,我找到了ConvertStringTobyTearray函数,该功能将符合字符串并将其转换为一个字节数组。

原始的C ++函数:

static int convertStringToByteArray(TCHAR *src, int destLength, BYTE *dest)
{
    TCHAR *dummy;
    unsigned int temp, i = 0;
    dummy = malloc(destLength*2*sizeof(TCHAR) + sizeof(TCHAR));
    _tcsncpy(dummy, src, destLength*2+1);
    dummy[destLength*2] = _T('\0');
    while (_stscanf(&(dummy[i*2]), _T("%02x"), &temp) > 0)
    {
        dest[i] = (BYTE)temp;
        i++;
    }
    free(dummy);
    return i;
}

我尝试在Delphi中编写类似的过程,我尝试以两种方式转换字符串 - 第一个(字面意思)将每个字符(被视为HEX)转换为整数。第二种方式使用移动:

procedure StrToByteArray(Input: AnsiString; var Bytes: array of Byte; Literally: Boolean = false);
var
  I, B : Integer;
begin
  if Literally then
  begin
    for I := 0 to Length(Input) -1 do
    if TryStrToInt('$' + Input[I +1], B) then
      Bytes[I] := Byte(B)
    else
      Bytes[I] := Byte(0);
  end else
    Move(Input[1], Bytes[0], Length(Input));
end;

但是我一直在得到

(6A82:找不到要选择的应用程序。)

错误,当我尝试选择援助时

a000000003000000

我知道我正在尝试选择正确的援助,因为当我对GPShell使用同样的帮助时,我会获得成功的响应。因此,我不确定问题是否在我的字符串到字节阵列过程中,或者是其他地方。

当我使用断点时,我尝试将字符串转换为字面上的字符号,我会进入

(10, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0)

调试器。但是,当我尝试移动(或ORD)时,我

(65, 48, 48, 48, 48, 48, 48, 48, 48, 51, 48, 48, 48, 48, 48, 48)

也尝试将字符串与各种网站在线转换为字符,这给了我另一个结果,

(41, 30, 30, 30, 30, 30, 30, 30, 30, 33, 30, 30, 30, 30, 30, 30)

因此我在试图找出我在做什么错的地方有些失去我的字符串转换中的问题 - 还是我需要看其他地方?

Im trying to use the GlobalPlatform library from Karsten Ohme (kaoh) in Delphi. Thanks to the help of some people here on stackoverflow i got it parially working and i am able to setup a connection to the cardreader. Now i am trying to select an AID, and therefore i need to pass the AID as array of Bytes to the function.

I use the GPShell (the commandline tool that uses the same library) source code as a reference to help me translate the functions. There i found the convertStringToByteArray function, which takes a string and converts it to a array of Bytes.

The original C++ function:

static int convertStringToByteArray(TCHAR *src, int destLength, BYTE *dest)
{
    TCHAR *dummy;
    unsigned int temp, i = 0;
    dummy = malloc(destLength*2*sizeof(TCHAR) + sizeof(TCHAR));
    _tcsncpy(dummy, src, destLength*2+1);
    dummy[destLength*2] = _T('\0');
    while (_stscanf(&(dummy[i*2]), _T("%02x"), &temp) > 0)
    {
        dest[i] = (BYTE)temp;
        i++;
    }
    free(dummy);
    return i;
}

and i tried to write a similar procedure in Delphi, i tried to convert the string in two ways - the first (Literally) converts every character (treated as HEX) to integer. The second way uses Move:

procedure StrToByteArray(Input: AnsiString; var Bytes: array of Byte; Literally: Boolean = false);
var
  I, B : Integer;
begin
  if Literally then
  begin
    for I := 0 to Length(Input) -1 do
    if TryStrToInt('

But i keep getting a

(6A82: The application to be selected could not be found.)

error, when i try to select AID

A000000003000000

I know i am trying to select the right AID, because when i use this same AID with GPShell - i get a successful response. So i am not sure if the problem is in my String to Byte array procedure, or maybe somewhere else.

When i use breakpoints, and i try to convert the string to bytes literally i get

(10, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0)

in the debugger. But when i try with Move (or ORD) i get

(65, 48, 48, 48, 48, 48, 48, 48, 48, 51, 48, 48, 48, 48, 48, 48)

I also tried to convert the string to bytes online with various websites, and these give me another result

(41, 30, 30, 30, 30, 30, 30, 30, 30, 33, 30, 30, 30, 30, 30, 30)

So i am a bit lost in trying to find out what im doing wrong, is the problem in my string to bytes conversion - or do i need to look somewhere else?

+ Input[I +1], B) then Bytes[I] := Byte(B) else Bytes[I] := Byte(0); end else Move(Input[1], Bytes[0], Length(Input)); end;

But i keep getting a

(6A82: The application to be selected could not be found.)

error, when i try to select AID

A000000003000000

I know i am trying to select the right AID, because when i use this same AID with GPShell - i get a successful response. So i am not sure if the problem is in my String to Byte array procedure, or maybe somewhere else.

When i use breakpoints, and i try to convert the string to bytes literally i get

in the debugger. But when i try with Move (or ORD) i get

I also tried to convert the string to bytes online with various websites, and these give me another result

So i am a bit lost in trying to find out what im doing wrong, is the problem in my string to bytes conversion - or do i need to look somewhere else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2025-01-27 14:42:36

原始的 C++ 代码将十六进制数字对解析为字节:

A000000003000000 -> A0 00 00 00 03 00 00 00

但是您的 Delphi 代码正在将单个十六进制数字解析为字节:

A000000003000000 -> A 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0

试试这个:

procedure StrToByteArray(Input: AnsiString; var Bytes: TBytes; Literally: Boolean = false);
var
  I, B : Integer;
begin
  if Literally then
  begin
    SetLength(Bytes, Length(Input) div 2);
    for I := 0 to Length(Bytes)-1 do
    begin
      if not TryStrToInt('

话虽这么说,Delphi 有自己的 HexToBin() 用于解析的函数十六进制字符串转换为字节数组。您不需要编写自己的解析器,例如:

procedure StrToByteArray(Input: AnsiString; var Bytes: TBytes; Literally: Boolean = false);
begin
  if Literally then
  begin
    SetLength(Bytes, Length(Input) div 2);
    HexToBin(PAnsiChar(Input), PByte(Bytes), Length(Bytes));
  end else
  begin
    SetLength(Bytes, Length(Input));
    Move(Input[1], Bytes[0], Length(Input));
  end:
end;
+ Copy(Input, (I*2)+1, 2), B) then B := 0; Bytes[I] := Byte(B); end; end else begin SetLength(Bytes, Length(Input)); Move(Input[1], Bytes[0], Length(Input)); end: end;

话虽这么说,Delphi 有自己的 HexToBin() 用于解析的函数十六进制字符串转换为字节数组。您不需要编写自己的解析器,例如:

The original C++ code parses pairs of hex digits into bytes:

A000000003000000 -> A0 00 00 00 03 00 00 00

But your Delphi code is parsing individual hex digits into bytes instead:

A000000003000000 -> A 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0

Try this instead:

procedure StrToByteArray(Input: AnsiString; var Bytes: TBytes; Literally: Boolean = false);
var
  I, B : Integer;
begin
  if Literally then
  begin
    SetLength(Bytes, Length(Input) div 2);
    for I := 0 to Length(Bytes)-1 do
    begin
      if not TryStrToInt('

That being said, Delphi has its own HexToBin() functions for parsing hex strings into byte arrays. You don't need to write your own parser, eg:

procedure StrToByteArray(Input: AnsiString; var Bytes: TBytes; Literally: Boolean = false);
begin
  if Literally then
  begin
    SetLength(Bytes, Length(Input) div 2);
    HexToBin(PAnsiChar(Input), PByte(Bytes), Length(Bytes));
  end else
  begin
    SetLength(Bytes, Length(Input));
    Move(Input[1], Bytes[0], Length(Input));
  end:
end;
+ Copy(Input, (I*2)+1, 2), B) then B := 0; Bytes[I] := Byte(B); end; end else begin SetLength(Bytes, Length(Input)); Move(Input[1], Bytes[0], Length(Input)); end: end;

That being said, Delphi has its own HexToBin() functions for parsing hex strings into byte arrays. You don't need to write your own parser, eg:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文