Python convert integer to byte

Convert a Python int into a big-endian string of bytes

I have a non-negative int and I would like to efficiently convert it to a big-endian string containing the same data. For example, the int 1245427 (which is 0x1300F3) should result in a string of length 3 containing three characters whose byte values are 0x13, 0x00, and 0xf3. My ints are on the scale of 35 (base-10) digits. How do I do this?

9 Answers 9

If you don’t want to specify the size

>>> n = 1245427 >>> n.to_bytes((n.bit_length() + 7) // 8, 'big') or b'\0' b'\x13\x00\xf3' 

If you don’t mind specifying the size

>>> (1245427).to_bytes(3, byteorder='big') b'\x13\x00\xf3' 

Am I right in thinking the (length + 7) // 8 part is the same as doing math.ceil(length / 8) ? If so, I think it’d be clearer to use that option

@JanusTroelsen They are the same, as long as the n.bit_length() is a reasonable size (you need around a quadrillion bits for there to be an issue, which means that n is going to be unbelievably large). In my situation readability is much more important than a performance increase, and numbers will never be that big.

Читайте также:  Windows form show html

@Kebman why would they be valid UTF-8? I don’t see why that would be a case. Maybe ask a new well-written question and ping me, and I’ll try to respond.

import struct print(struct.pack('>I', your_int)) 

‘>I’ is a format string. > means big endian and I means unsigned int. Check the documentation for more format chars.

struct.pack returns a fixed length string and doesn’t seem to have facilities for handling large ints. I suppose I could break my int into powers of 2^32, run it through struct.pack(), and reassemble the result, but that seems like a lot of work. do you know of a simpler way?

I couldn’t find a library to handle arbitrary long ints. I think you will have to implement it yourself. Other answers contain implementations.

Ayman, note that Python has built-in support for arbitrarily long ints, so you don’t need a library. In Python 3, there will only be the int type, but even now in Python 2.4+, int s are automatically converted to Python long s when they overflow 32 bits (signed).

benhoyt, thanks for the comment. I’m aware of this. I was talking about handling the conversion of arbitrary long ints to big endian. Not handling them in general.

This is fast and works for small and (arbitrary) large ints:

def Dump(n): s = '%x' % n if len(s) & 1: s = '0' + s return s.decode('hex') print repr(Dump(1245427)) #: '\x13\x00\xf3' 

As a variant of the above, one can replace if len(s) & 1 with if len(s) % 2 (both are ture if there are an odd amount of hexadecimal characters), and ‘%x’ % n with ‘<0:x>‘.format(n) (both of which format the number as a hexadecimal string).

Probably the best way is via the built-in struct module:

>>> import struct >>> x = 1245427 >>> struct.pack('>BH', x >> 16, x & 0xFFFF) '\x13\x00\xf3' >>> struct.pack('>L', x)[1:] # could do it this way too '\x13\x00\xf3' 

Alternatively — and I wouldn’t usually recommend this, because it’s mistake-prone — you can do it «manually» by shifting and the chr() function:

>>> x = 1245427 >>> chr((x >> 16) & 0xFF) + chr((x >> 8) & 0xFF) + chr(x & 0xFF) '\x13\x00\xf3' 

Out of curiosity, why do you only want three bytes? Usually you’d pack such an integer into a full 32 bits (a C unsigned long ), and use struct.pack(‘>L’, 1245427) but skip the [1:] step?

Источник

Python convert integer to byte

Last updated: Jun 23, 2023
Reading time · 3 min

banner

# Table of Contents

# How to convert Int to Bytes in Python

Use the int.to_bytes() method to convert an integer to bytes in Python.

The method returns an array of bytes representing an integer.

Copied!
num = 2048 my_bytes = num.to_bytes(2, byteorder='big') print(my_bytes) # 👉️ b'\x08\x00'

convert int to bytes in python

If your integer is not stored in a variable, make sure to wrap it in parentheses before calling to_bytes() .

Copied!
my_bytes = (2048).to_bytes(2, byteorder='big') print(my_bytes) # 👉️ b'\x08\x00'

The int.to_bytes method returns an array of bytes representing an integer.

The integer is represented using length bytes and defaults to 1 .

An OverflowError is raised if the integer cannot be represented using the given number of bytes.

Copied!
my_bytes = (1024).to_bytes(2, byteorder='big') print(my_bytes) # 👉️ b'\x04\x00' my_bytes = (1024).to_bytes(10, byteorder='big') print(my_bytes) # 👉️ b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'

convert int to bytes specifying length

The second argument we passed to the to_bytes() method determines the byte order that is used to represent the integer.

The byteorder argument defaults to big .

If the byteorder argument is set to big , then the most significant byte is at the beginning of the byte array.

If you set the byteorder argument to little , the most significant byte is at the end of the byte array.

Copied!
my_bytes = (1024).to_bytes(2, byteorder='little') print(my_bytes) # 👉️ b'\x00\x04' my_bytes = (1024).to_bytes(10, byteorder='little') print(my_bytes) # 👉️ b'\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00'

set byteorder to little

# Creating a reusable function to convert an integer to bytes and vice versa

You can also create a reusable function that converts integers to bytes.

Copied!
def int_to_bytes(integer): return integer.to_bytes((integer.bit_length() + 7) // 8, 'big') print(int_to_bytes(65)) # 👉️ b'A' print(int_to_bytes(1024)) # 👉️ b'\x04\x00' print(int_to_bytes(2048)) # 👉️ b'\x08\x00'

The int_to_bytes function takes an integer as a parameter and converts it to a bytes object.

Conversely, if you need to convert a bytes object to an integer, use the int.from_bytes() method instead.

Copied!
def int_from_bytes(bytes_obj): return int.from_bytes(bytes_obj, byteorder='big') print(int_from_bytes(b'A')) # 👉️ b'A' print(int_from_bytes(b'\x04\x00')) # 👉️ b'\x04\x00' print(int_from_bytes(b'\x08\x00')) # 👉️ b'\x08\x00'

The int.from_bytes() method returns the integer represented by the given byte array.

The first argument the method takes must be a bytes-like object or an iterable that produces bytes.

The byteorder argument determines which byte order is used to represent the integer.

The default value is big which means that the most significant byte is at the beginning of the byte array.

If the byteorder argument is set to «little» , then the most significant byte is at the end of the byte array.

# Converting signed (negative) integers to bytes in Python

The examples above only work for unsigned (non-negative integers).

If you need to convert signed integers to bytes, use the following function instead.

Copied!
def int_to_bytes(integer): return integer.to_bytes( length=(8 + (integer + (integer 0)).bit_length()) // 8, byteorder='big', signed=True ) print(int_to_bytes(-1024)) # 👉️ b'\xfc\x00' print(int_to_bytes(-2048)) # 👉️ b'\xf8\x00'

Calculating the length argument when converting signed (negative) integers to bytes is a bit more complicated.

The signed argument determines whether two’s complement is used to represent the integer.

If signed is False and a negative integer is supplied, an OverflowError is raised.

By default, the signed argument is set to False .

The following function can be used if you need to convert signed bytes to integers.

Copied!
def int_from_bytes(binary_data): return int.from_bytes(binary_data, byteorder='big', signed=True) print(int_from_bytes(b'\xfc\x00')) # -1024 print(int_from_bytes(b'\xf8\x00')) # -2048

The signed argument indicates whether two’s complement is used to represent the integer.

# Converting the Integer to a String and then Bytes

If you need to convert the integer to a string and then bytes, use the str.encode method.

Copied!
num = 2048 my_bytes = str(num).encode(encoding='utf-8') print(my_bytes) # 👉️ b'2048'

We passed the integer to the str class to convert it to a string and then used the str.encode() method to convert the string to bytes.

The same can be achieved by using the bytes class.

Copied!
num = 2048 my_bytes = bytes(str(num), encoding='utf-8') print(my_bytes) # 👉️ b'2048'

If you need to convert the bytes object back to an integer, use the bytes.decode method and the int() class.

Copied!
num = 2048 my_bytes = str(num).encode(encoding='utf-8') print(my_bytes) # 👉️ b'2048' my_int = int(my_bytes.decode(encoding='utf-8')) print(my_int) # 👉️ 2048

The bytes.decode() method converts the bytes object to a string.

The last step is to use the int() class to convert the string to an integer.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Integer to byte conversion

Say I’ve got an integer, 13941412, that I wish to separate into bytes (the number is actually a color in the form 0x00bbggrr). How would you do that? In c, you’d cast the number to a BYTE and then shift the bits. How do you cast to byte in Python?

3 Answers 3

Use bitwise mathematical operators, the «bytes» are already there:

def int_to_rgb(n): b = (n & 0xff0000) >> 16 g = (n & 0x00ff00) >> 8 r = (n & 0x0000ff) return (r, g, b) 

You can bitwise & with 0xff to get the first byte, then shift 8 bits and repeat to get the other 3 bytes.

Edit: For colors, you know you need the least significant three bytes. So, you can use a nicer approach:

r = num & 0x0000ff g = (num & 0x00ff00) >> 8 b = (num & 0xff0000) >> 16 

In order to just pull out the bytes for each color, going with msw’s approach will be simpler. I’ll leave this post as a general approach to pulling out the bytes.

Here’s an optimisation suggestion that applies in any language, and doesn’t harm legibility.

b = (n & 0xff0000) >> 16 g = (n & 0xff00) >> 8 r = (n & 0xff) 
b = (n >> 16) & 0xff g = (n >> 8) & 0xff r = n & 0xff 

Having fewer constants is not slower, and may be faster.

Having smaller constants is not slower, and may be faster — in a language like C, a shorter machine instruction may be available; in a language like Python, the implementation is likely to pool small integers.

Источник

Python 2,3 Convert Integer to «bytes» Cleanly

I am particularly concerned with two factors: readability and portability. The second method, for Python 3, is ugly. However, I think it may be backwards compatible. Is there a shorter, cleaner way that I have missed? I currently make a lambda expression to fix it with a new function, but maybe that’s unnecessary.

I realize that you are trying to be broad to keep it from being to specific, but it’s hard to tell why you’re trying to do this or what exactly you’re trying to achieve.

@AshwiniChaudhary, I tried it at trypython.org/#, because my current Python is 3.3, but I acknowledge that that’s hardly conclusive.

When bytes exists in 2, it’s only an alias for str . bytearray exists, though, and has a bytearray(string, encoding[, errors]) -> bytearray call signature.

I think what @IgnacioVazquez-Abrams meant was that a network protocol would be more likely to want binary 0x05 rather than ASCII ‘5’ , but I know there are plenty of protocols that are text based. As for the double conversion to Unicode then raw bytes, it’s eminently sensible when you’ve experienced the alternative. In Python 2 it’s far too easy to get confused about whether you have a string of characters or a string of bytes and it leads to no end of bugs.

7 Answers 7

To convert a string to a sequence of bytes in either Python 2 or Python 3, you use the string’s encode method. If you don’t supply an encoding parameter ‘ascii’ is used, which will always be good enough for numeric digits.

In Python 2 str(n) already produces bytes; the encode will do a double conversion as this string is implicitly converted to Unicode and back again to bytes. It’s unnecessary work, but it’s harmless and is completely compatible with Python 3. Answer 2:

Above is the answer to the question that was actually asked, which was to produce a string of ASCII bytes in human-readable form. But since people keep coming here trying to get the answer to a different question, I’ll answer that question too. If you want to convert 10 to b’10’ use the answer above, but if you want to convert 10 to b’\x0a\x00\x00\x00′ then keep reading.

The struct module was specifically provided for converting between various types and their binary representation as a sequence of bytes. The conversion from a type to bytes is done with struct.pack . There’s a format parameter fmt that determines which conversion it should perform. For a 4-byte integer, that would be i for signed numbers or I for unsigned numbers. For more possibilities see the format character table, and see the byte order, size, and alignment table for options when the output is more than a single byte.

import struct s = struct.pack(' 

Источник

Оцените статью