Pla*_*ort 0 delphi pascal endianness mifare
怎么把这个...
我整天都在努力反转Mifare NFC卡的UID值,该值我读为十六进制字符串,可以将其转换为整数,这对于4字节UID来说效果很好。
可能有大型UID示例的mifare desfire卡会变得一团糟:
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
Run Code Online (Sandbox Code Playgroud)
所以这里有一些严重缺失的东西...请原谅
我怀疑IntToHex函数有问题
您的HexToInt()函数返回LongWord,因此其输出取决于平台。对于像Linux和iOS这样的64位POSIX平台,它是8个字节,对于其他平台,它是4个字节。
此外,LongEndian()即使在8字节上进行操作,您的函数也仅交换4字节UInt64。
因此,您应该:
修复这些功能以在所有平台上的全部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;
Run Code Online (Sandbox Code Playgroud)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;
Run Code Online (Sandbox Code Playgroud)话虽这么说,第三个选择是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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
156 次 |
| 最近记录: |