chr (n) falló cuando n excede 256

Estoy utilizando chr y int to convert a binary representation string to real binary value.

Lo que hice es


Entonces binary is the real binary value of the representation string.

But what if the string I gave to int is larger than 256 in decimal?

¿Alguna solución?

PS: The reason I use chr is I want to convert n to a string so that I can write it to a file, using f.write().

And this question is originated from esta pregunta

preguntado el 27 de agosto de 11 a las 14:08

4 Respuestas

chr() is defined as operating on ASCII characters, which must be less than 256 in value. If you are expecting Unicode characters, you can use unichr() en vez.

Respondido 27 ago 11, 18:08

If the value is more than 256, mod 256 will be returned. - dayuloli

You can use different format characters in the format string given to the pack() método en el struct module to easily get the corresponding binary representation string of signed or unsigned integers from 1 to 8 bytes in length.

>>> from struct import pack
>>> pack('B', 255)
>>> pack('H', 257)
>>> pack('Q', 9223372036854775807)

The value returned can be written to a file using f.write() if desired. When reading or writing binary data to a file, you should append 'b' to the mode argument value when you open() el archivo.

Respondido 27 ago 11, 20:08

If I understand your question you want to convert a binary string to an integer value. That is what you are doing in the first line of code. The second line just converts an integer value to the character represented by that value in the ASCII table. So, for example if the string is 01100001 that will be converted to an int value of 97 in the first step. Tue second step will then convert 97 to the ASCII character 'a'. If you then try to use this variable as a number it will be converted back to the ing value of 97. So, if I understand your question you actually have your desired number after step 1.

Respondido 27 ago 11, 19:08

I don't think that's what his goal is. He wants to take 01000001, convert than to 65, and then convert that to a, which will be written to a file. - dlev

@Anulith, yes, dlev is right. The ultimate goal is to write it to a file. So my roadmap is 1, convert string to int, 2, int to string so it can be written to file, 3, write to file - xiaohan2012

When trying to convert unicode values you can run into a multitude of problems. So using unichr is not always possible either, for example:

>>> n = int('0001f600', 16)
>>> unichr(n)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unichr() arg not in range(0x10000) (narrow Python build)

Note la narrow Python build message in the error, this means Python was built without wide unicode characters support, but even narrow Python can get around this limitation (without having to be recompiled with the --enable-unicode=ucs4 bandera):

>>> n = int('0001f600', 16)
>>> s = '\\U{:0>8X}'.format(n)
>>> s
>>> binary = s.decode('unicode-escape')
>>> print(binary)

Respondido el 09 de diciembre de 17 a las 01:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.