创建一个将十六进制值作为字符串反向排序的函数

时间:2019-06-12 20:14:34

标签: delphi pascal endianness mifare

如何放置这个...

我整天都在努力反转Mifare NFC卡的UID值,我将该值读取为十六进制字符串,可以将其转换为整数,这对于4字节UID来说效果很好。

关于mifare desfire卡,事情可能会变得很混乱,该卡可能会有很大的UID示例:

04 44 44 22 E0 62 80

可以正确读取值,我甚至可以使用函数StrToInt('$'+ TheUIDvalue)将其转换为十进制

现在很奇怪的是,我需要使用以下函数将其反转:

function HexToInt(s: string): Longword;
var
  b: byte;
  c: Char;
begin
  Result := 0;
  s := UpperCase(s);
  for b := 1 to Length(s) do
  begin
    Result := Result * 16;
    c := s[b];
    case c of
      '0' .. '9':
        Inc(Result, Ord(c) - Ord('0'));
      'A' .. 'F':
        Inc(Result, Ord(c) - Ord('A') + 10);
    else
      begin
        Result := 0
      end;
    end;
  end;
end;

function LongEndian(L : UInt64) : UInt64;
begin
  result := ((L and $000000FF) shl 24) or
            ((L and $0000FF00) shl 8) or
            ((L and $00FF0000) shr 8) or
            ((L and $FF000000) shr 24);
end;

function TfrmMain.HexResponseReversed ( input: string): string;
begin
  result := IntToHex(EndianLong(hexToInt(input)));
end;



The result i get is : 
         80 62 E0 22
04 44 44 22 E0 62 80

所以这里有一些严重缺失的东西...请原谅凌乱的代码

我怀疑IntToHex函数有问题

1 个答案:

答案 0 :(得分:6)

您的HexToInt()函数返回一个LongWord,因此其输出取决于平台。对于Linux和iOS等64位POSIX平台,它是8字节,对于其他平台,它是4字节。

此外,即使对8字节的LongEndian()进行操作,您的UInt64函数也仅交换4个字节。

因此,您应该:

  • 修复这些功能以在所有平台上的全部8个字节上运行,例如:

    function HexToInt64(s: string): UInt64;
    var
      Len, I: Integer;
      c: Char;
    begin
      Result := 0;
      Len := Length(s);
      if Len = 0 then Exit;
      if Odd(Len) then
      begin
        s := '0' + s;
        Inc(Len);
      end;
      if Len > 16 then Exit;
      for I := 1 to Len do
      begin
        Result := Result * 16;
        c := s[I];
        case c of
          '0' .. '9':
            Inc(Result, Ord(c) - Ord('0'));
          'A' .. 'F':
            Inc(Result, Ord(c) - Ord('A') + 10);
          'a' .. 'f':
            Inc(Result, Ord(c) - Ord('a') + 10);
        else
          begin
            Result := 0;
            Exit;
          end;
        end;
      end;
    end;
    
    function EndianLong(L : UInt64) : UInt64;
    begin
      Result := ((L and $00000000000000FF) shl 56) or
                ((L and $000000000000FF00) shl 40) or
                ((L and $0000000000FF0000) shl 24) or
                ((L and $00000000FF000000) shl 8) or
                ((L and $000000FF00000000) shr 8) or
                ((L and $0000FF0000000000) shr 24) or
                ((L and $00FF000000000000) shr 40) or
                ((L and $FF00000000000000) shr 56);
    end;
    
    function TfrmMain.HexResponseReversed(input: string): string;
    begin
      Result := IntToHex(EndianLong(HexToInt64(input)));
    end;
    
  • 将十六进制字符串的各个HH对解码为字节数组,然后交换数组元素的顺序,然后从数组元素创建新的十六进制字符串:

    uses
      ..., Classes;
    
    function HexToBytes(s: string): TBytes;
    var
      Len: Integer;
    begin
      Result := nil;
      Len := Length(s);
      if Len = 0 then Exit;
      if Odd(Len) then
      begin
        s := '0' + s;
        Inc(Len);
      end;
      Len := Len div 2;
      SetLength(Result, Len);
      if HexToBin(PChar(s), PAnsiChar(PByte(Result)), Len) <> Len then
        SetLength(Result, 0);
    end;
    
    function BytesToHex(Bytes: TBytes): string;
    var
      Len: Integer;
    begin
      Result := nil;
      Len := Length(Bytes);
      if Len = 0 then Exit;
      SetLength(Result, Len * 2);
      BinToHex(PAnsiChar(PByte(Bytes)), PChar(Result), Len);
    end;
    
    function EndianLong(Bytes : TBytes) : TBytes;
    var
      Len, I, J: Integer;
      B: Byte;
    begin
      Result := nil;
      Len := Length(Bytes); 
      if Len = 0 then Exit;
      Result := Copy(Bytes, 0, Len);
      if Len = 1 then Exit;
      J := Pred(Len);
      for I := 0 to Pred(Len div 2) do
      begin
        B := Result[I];
        Result[I] := Result[J];
        Result[J] := B;
        Dec(J);
      end;
    end;
    
    function TfrmMain.HexResponseReversed(input: string): string;
    begin
      Result := BytesToHex(EndianLong(HexToBytes(input)));
    end;
    

话虽如此,第三个选择是直接交换输入字符串的HH对,而根本不将整个输入字符串转换为整数或字节数组:

uses
  ..., SysUtils;

function TfrmMain.HexResponseReversed(input: string): string;
const
  HexDigits = ['0'..'9', 'A'..'F', 'a'..'f'];
var
  Len, I: Integer;
  P, Q: PChar;
  Hex: array[0..1] of Char;
begin
  Result := '';
  Len := Length(input);
  if Len = 0 then Exit;
  Result := input;
  if Odd(Len) then
  begin
    Result := '0' + Result;
    Inc(Len);
  end;
  Len := Len div 2;
  if Len = 1 then Exit;
  UniqueString(Result);
  P := PChar(Result);
  Q := AnsiLastChar(Result);
  Dec(Q);
  For I := 1 to Len div 2 do
  begin
    Move(P^, Hex, SizeOf(Hex));
    if not ((Hex[0] in HexDigits) and (Hex[1] in HexDigits)) then
    begin
      Result := '';
      Exit;
    end;
    Move(Q^, P^, SizeOf(Hex));
    Move(Hex, Q^, SizeOf(Hex));
    Inc(P, 2);
    Dec(Q, 2);
  end;
end;