Plastkort
Plastkort

Reputation: 967

Finding the correct formula for encoded hex value in decimal

I have a case here where I am trying to figure out how a hex number is converted into a decimal number.

I had a similar case before, but found out that if I reversed the hex string, and swapped each second value (little-endian), then converting it back to a decimal value I got what I wanted, but this one is different.

here is the values we received

Value nr. 1 is Dec: 1348916578 Hex: 0a66ab46

I just have this one decimal/hex for now but I am trying to get more values to compare results.

I hope any math genius out there will be able to see what formula might been used here :)

thanks

Upvotes: 3

Views: 1493

Answers (4)

Mikelangelo
Mikelangelo

Reputation: 917

What we found was that our min USB reader that gave 10 bit decimal format is actually not showing the whole binary code. The hexadecimal reader finds the full binary code. so essentially it is possible to convert from hexadecimal value to 10 bit decimal by taking off 9 characters after binary conversion.

But this does not work the other way around (unless we strip away 2 characters from the hexadecimal value the 10 bit decimal code will only show part of the full binary code).

So case closed.

Upvotes: 0

Chris Thornton
Chris Thornton

Reputation: 15817

Interesting.... I expanded the decimal and hex into binary, and this is what you get, respectively:

1010000011001101101010101100010
1010011001101010101101000110

Slide the bottom one over by padding with some 0s, then split into 8-byte blocks.

10100000 1100110 11010101 01100010
10100    1100110 10101011 01000110

It seems to start to line up. Let's make the bottom look like the top.

Pad the first block with 0s and it's equal. The second block is ok. Switch the 3rd block around (reverse it) and 10101011 becomes 11010101.

10100000 1100110 11010101 01000110

Likewise with the 4th.
10100000 1100110 11010101 01100010

Now they're the same.

10100000 1100110 11010101 01100010
10100000 1100110 11010101 01100010

Will this work for all cases? Impossible to know.

Upvotes: 4

Barry Kelly
Barry Kelly

Reputation: 42152

1348916578
= 5    0    6    6     D    5    6    2 hex
= 0101 0000 0110 0110  1101 0101 0110 0010

0a66ab46
= 0    A    6    6     A    B    4    6 hex
= 0000 1010 0110 0110  1010 1011 0100 0110

So, if a number is like this, in hex digits:

AB CD EF GH

Then a possible conversion is:

rev(B) rev(A) rev(D) rev(C) rev(F) rev(E) rev(H) rev(G)

where rev reverses the order of bits in the nibble; though I can see that the reversal could be done on a byte-wise basis also.

Upvotes: 11

fmotis
fmotis

Reputation: 286

The decimal value of x0a66ab46 is 174500678 or 1185637898 (depending which endian you use, with any 8, 16 or 32bit access). There seems to be no direct connection between these values. Maybe you just have the pair wrong? It could help if you posted some code about how you generate these value pairs.

BTW, Delphi has a fine little method for this: SysUtils.IntToHex

Upvotes: 0

Related Questions