3

The answers to this question make it seem like there are two ways to convert an integer to a bytes object in Python 3. They show

s = str(n).encode()

and

n = 5 bytes( [n] ) 

Being the same. However, testing that shows the values returned are different:

print(str(8).encode()) #Prints b'8' 

but

print(bytes([8])) #prints b'\x08' 

I know that the first method changes the int 8 into a string (utf-8 I believe) which has the hex value of 56, but what does the second one print? Is that just the hex value of 8? (a utf-8 value of backspace?)

Similarly, are both of these one byte in size? It seems like the second one has two characters == two bytes but I could be wrong there...

3
  • the \x in \x08 is just indicating that the 08 is a hex number. Both results are one byte. Commented Aug 26, 2014 at 0:01
  • 1
    How do the answers show them being the same? The answers make it very clear that they're doing different things—one returns the ASCII-encoded string representation of the number 8, the other returns the byte 8. These are not at all the same. Commented Aug 26, 2014 at 0:03
  • 1
    Also, if you want to know how long the second one is, why not ask it: print(len(bytes([8]))) will give you 1. It is just the single UTF-8 value of backspace, as you suspected, which is one byte, not two. Commented Aug 26, 2014 at 0:06

2 Answers 2

5

b'8' is a bytes object which contains a single byte with value of the character '8' which is equal to 56.

b'\x08' is a bytes object which contains a single byte with value 8, which is the same as 0x8.

Sign up to request clarification or add additional context in comments.

Comments

3

Those two examples are not equivalent. str(n).encode() takes whatever you give it, turns it into its string representation, and then encodes using a character codec like utf8. bytes([..]) will form a bytestring with the byte values of the array given. The representation \xFF is in fact the hexadecimal representation of a single byte value.

>>> str(8).encode() b'8' >>> b'8' == b'\x38' True 

2 Comments

When I do str(8) it just returns 8 as a string, not a byte object. When you say "byte values" do you mean their decimal values?
My apologies, I omitted the .encode by accident. However, you should note that what that .encode() does is take the unicode string and transform it to an 8-bit string (i.e. a bytestring) using a character codec; in the end it still represents text. When I say an array of "byte values", I do mean decimals. Specifically integers in the range [0,255]. Any value greater than 255 or less than 0 would give you an error, because it's not a byte!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.