Python - Decimal to Hex, Reverse byte order, Hex to Decimal

39,404

Solution 1

Bit shifting to swap upper/lower eight bits:

>>> x = 36895
>>> ((x << 8) | (x >> 8)) & 0xFFFF
8080

Packing and unpacking unsigned short(H) with opposite endianness(<>):

>>> struct.unpack('<H',struct.pack('>H',x))[0]
8080

Convert 2-byte little-endian to big-endian...

>>> int.from_bytes(x.to_bytes(2,'little'),'big')
8080

Solution 2

Print formatting also works with strings.

# Get the hex digits, without the leading '0x'
hex_str = '%04X' % (36895)

# Reverse the bytes using string slices.
# hex_str[2:4] is, oddly, characters 2 to 3.
# hex_str[0:2] is characters 0 to 1.
str_to_convert = hex_str[2:4] + hex_str[0:2]

# Read back the number in base 16 (hex)
reversed = int(str_to_convert, 16)

print(reversed) # 8080!

Solution 3

My approach


import binascii

n = 36895
reversed_hex = format(n, 'x').decode('hex')[::-1]
h = binascii.hexlify(reversed_hex)
print int(h, 16)

or one line

print int(hex(36895)[2:].decode('hex')[::-1].encode('hex'), 16)
print int(format(36895, 'x').decode('hex')[::-1].encode('hex'), 16)
print int(binascii.hexlify(format(36895, 'x').decode('hex')[::-1]), 16)

or with bytearray

import binascii

n = 36895
b = bytearray.fromhex(format(n, 'x'))
b.reverse()
print int(binascii.hexlify(b), 16)

Solution 4

Keep in mind that 'hex'(base 16 0-9 and a-f) and 'decimal'(0-9) are just constructs for humans to represent numbers. It's all bits to the machine.

The python hex(int) function produces a hex 'string' . If you want to convert it back to decimal:

>>> x = 36895
>>> s = hex(x)
>>> s
'0x901f'
>>> int(s, 16)  # interpret s as a base-16 number

Solution 5

To convert from decimal to hex, use:

dec = 255
print hex(dec)[2:-1]

That will output the hex value for 255. To convert back to decimal, use

hex = 1F90
print int(hex, 16)

That would output the decimal value for 1F90.

You should be able to reverse the bytes using:

hex = "901F"
hexbyte1 = hex[0] + hex[1]
hexbyte2 = hex[2] + hex[3]
newhex = hexbyte2 + hexbyte1
print newhex

and this would output 1F90. Hope this helps!

Share:
39,404

Related videos on Youtube

Jim
Author by

Jim

Updated on March 12, 2021

Comments

  • Jim
    Jim about 3 years

    I've been reading up a lot on stuct.pack and hex and the like.

    I am trying to convert a decimal to hexidecimal with 2-bytes. Reverse the hex bit order, then convert it back into decimal.

    I'm trying to follow these steps...in python

    Convert the decimal value **36895** to the equivalent 2-byte hexadecimal value:
    
    **0x901F**
    Reverse the order of the 2 hexadecimal bytes:
    
    **0x1F90**
    Convert the resulting 2-byte hexadecimal value to its decimal equivalent:
    
    **8080**
    
    • Dingo
      Dingo about 13 years
      There is no such thing as hexadecimal and decimal values. They are just ways of displaying a value. "36895" and "0x901F" are the same value shown in different ways.
    • Jim
      Jim about 13 years
      struct.unpack('<H',struct.pack('>H',x))[0]
    • bobpaul
      bobpaul about 12 years
      It looks like you're changing endianness, not reversing the bit order. Reversing the bit order would be like changing 0xAC to 0x35. You're swapping bytes. I'd be really appreciated if you could update title as it comes up in the wrong Google searches.
  • Henry
    Henry about 13 years
    What if it's a smaller number, eg hex(10) == '0xa' so your code will break. Doing it in a stringy way is messy.
  • Henry
    Henry about 13 years
    Sure, so I guess you win pedant award, however I still think it's a messy way to accomplish this and does not demonstrate an understanding of the math that's going on, which is probably what this question is all about (doesn't it sound like homework to you?)
  • tew
    tew about 13 years
    I agree, it does sound like homework, and I agree, it is a messy way, but hey, for his purposes, it works. :)