Hex到Dec转换问题 [英] Hex to Dec conversion issue

查看:225
本文介绍了Hex到Dec转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能告诉我用什么转换规则来转换下面的数字?



0B2AA4CC> 0222646835

00AA62CD> 0005596219

0A24ACD0> 0088232880



这两个数字都是来自EM Marine 125kHz ID卡的UID(可能是EM4102),

左边的字符串是来自现有数据库的HEX编号和右边从USB读卡器(HID设备)收集字符串。



USB读卡器应该使用8H10D输出,但是当我尝试转换左边数字时它不匹配:

Hi,
can anyone tell me what conversion rule is used to convert below numbers ?

0B2AA4CC > 0222646835
00AA62CD > 0005596219
0A24ACD0 > 0088232880

Both numbers are UID from EM Marine 125kHz ID card (probably EM4102),
left string is HEX number from existing DB and right string is gathered from USB card reader (HID device).

USB card reader should use 8H10D output, but it doesn't match when I try convert left number like:

Console.WriteLine(BitConverter.ToUInt32(new byte[] { 0xCC, 0xA4, 0x2A, 0x0B }, 0)); 



我也尝试使用little / big / middle endian,但没有成功。


I also tried to use little/big/middle endian, but no success.

推荐答案

同时写两个值为十六进制,并排,然后作为位,例如

Write both values as hex, side by side, then as bits, e.g.
             0B2AA4CC = 0000 1011 0010 1010 1010 0100 1100 1100
0222646835 = 0D455233 = 0000 1101 0100 0101 0101 0010 0011 0011



你看到了这种模式吗?位序列是反向的(每个半字节)。



1)取8位十六进制数

2)将每个十六进制数转换为反向位序列值

3)从中创建一个10位十进制数

4)全部放在一起 8位十六进制> 10位数秒





一些密集的代码显示转换工作。

通过使用输入值的最低半字节并将该输入值右移为每个下一个半字节来查找要反转的半字节。锁定的值然后向左移动到相应的位置并设置为目标值。


Do you see the pattern? The bit sequence is inverse (per nibble).

1) take the 8 digit hex number
2) convert each hex digit to the reverse bit sequence value
3) create a 10 digit decimal number from it
4) put all together 8-bit-hex > 10-digit-dec


Some dense code to show the conversion working.
The nibbles to inverse are looked up by using the lowest nibble of the input value and shifting that input value right for each next nibble. The locked up value is then shifted left to the respective position and set into the target value.

class Program
{
    static readonly UInt32[] LOOKUP =
    { 0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF };
    static UInt32 Convert8HTo10D(UInt32 val8H)
    {
        UInt32 val10D = 0x0;
        for (int pos = 0; pos < 32; pos += 4, val8H >>= 4) val10D |= LOOKUP[val8H & 0xF] << pos;
        return val10D;
    }
    static void Write8H10D(UInt32 val8H)
    {
        Console.WriteLine("{0:X08} > {1:D10}", val8H, Convert8HTo10D(val8H));
    }
    static void Main(string[] args)
    {
        Write8H10D(0x0B2AA4CC);
        Write8H10D(0x00AA62CD);
        Write8H10D(0x0A24ACD0);
    }
}

这导致

0B2AA4CC > 0222646835
00AA62CD > 0005596219
0A24ACD0 > 0088232880

[/编辑]



干杯

Andi

[/EDIT]

Cheers
Andi


事实上,两个任何编码的值显示相同的十进制(?)结果表明这里没有进行线性转换。我假设您将需要设备制造商有关内部格式的信息。
The fact that two of the whatever-encoded values show the same decimal (?) result indicates no linear conversion is going on here. I assume you are going to need the device manufacturer's information on the internal formats.


这篇关于Hex到Dec转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆