Code Monkey home page Code Monkey logo

bytes-to-int-jsong's Introduction

Bytes to Int & more Python tips and tricks

Useful Python 3 Idioms

You can reverse a list by using [::-1]:

a = [1, 2, 3, 4, 5]
print(a[::-1]) # [5, 4, 3, 2, 1]

Also works on both strings and bytes:

s = 'hello world'
print(s[::-1]) # 'dlrow olleh'
b = b'hello world'
print(b[::-1]) # b'dlrow olleh'

Indexing bytes will get you the numerical value:

print(b'&'[0]) # 38 since & charcter #38

You can do the reverse by using bytes:

print(bytes([38])) # b'&'

Try it

a = [1, 2, 3, 4, 5]
print(a[::-1]) # [5, 4, 3, 2, 1]

s = 'hello world'
print(s[::-1]) # 'dlrow olleh'
b = b'hello world'
print(b[::-1]) # b'dlrow olleh'

print(b'&'[0]) # 38 since & charcter #38

print(bytes([38])) # b'&'

Python Tricks

Here is how we convert binary to/from hex:

print(b'hello world'.hex())
print(bytes.fromhex('68656c6c6f20776f726c64'))

Try it

Reverse this hex dump: b010a49c82b4bc84cc1dfd6e09b2b8114d016041efaf591eca88959e327dd29a

Hint: you'll want to turn this into binary data, reverse and turn it into hex again

h = 'b010a49c82b4bc84cc1dfd6e09b2b8114d016041efaf591eca88959e327dd29a'

# convert to binary (bytes.fromhex)
# reverse ([::-1])
# convert to hex()

Converting from bytes to int and back

Converting from bytes to integer requires learning about Big and Little Endian encoding. Essentially any number greater than 255 can be encoded in two ways, with the "Big End" going first or the "Little End" going first.

Normal human reading is from the "Big End". For example 123 is read as 100 + 20 + 3. Some computer systems encode integers with the "Little End" first.

A number like 500 is encoded this way in Big Endian:

0x01f4 (256 + 244)

But this way in Little Endian:

0xf401 (244 + 256)

In Python we can convert an integer to big or little endian using a built-in method:

n = 1234567890
big_endian = n.to_bytes(4, 'big')  # b'\x49\x96\x02\xd2'
little_endian = n.to_bytes(4, 'little')  # b'\xd2\x02\x96\x49'

We can also convert from bytes to an integer this way:

big_endian = b'\x49\x96\x02\xd2'
n = int.from_bytes(big_endian, 'big')  # 1234567890
little_endian = b'\xd2\x02\x96\x49'
n = int.from_bytes(little_endian, 'little')  # 1234567890
n = 1234567890
big_endian = n.to_bytes(4, 'big')
little_endian = n.to_bytes(4, 'little')

print(big_endian.hex())
print(little_endian.hex())

print(int.from_bytes(big_endian, 'big'))
print(int.from_bytes(little_endian, 'little'))

Try it

Convert the following:

  • 8675309 to 8 bytes in big endian
  • interpret b'\x11\x22\x33\x44\x55' as a little endian integer
n = 8675309
# print n in 8 big endian bytes

little_endian = b'\x11\x22\x33\x44\x55'
# print little endian in decimal

Test Driven Exercise

Add little_endian_to_int() and int_to_little_endian() methods to your library.

def little_endian_to_int(b):
    '''little_endian_to_int takes byte sequence as a little-endian number.
    Returns an integer'''
    # use the from_bytes method of int
    pass

def int_to_little_endian(n, length):
    '''endian_to_little_endian takes an integer and returns the little-endian
    byte sequence of length'''
    # use the to_bytes method of n
    pass

bytes-to-int-jsong's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.