0

I worked all day with no result in converting integer to the string of form "\x.." at the Python 3.2. When ascii conversion was used or other I get '0x..' or '\\x..', which is not proper for me. Any bytes or unicode ("\u92") adding operations results with "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-2: end of string in escape sequence"

>>>bytes([92])
b'\\'

>>>chr(92) + "x" + str(42)
'\\x42' 

>>> str(hex(66))
'0x42'

>>>ascii(bytes([255])).replace(r"'b\", "")
File "<stdin>", line 1
   ascii(bytes([255])).replace(r"'b\", "")
                                      ^
SyntaxError: invalid syntax

>>> "\x".encode('raw_unicode_escape').decode('ascii')
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence
user1536379
  • 1
  • 1
  • 1
  • 1
    Please give an example of the input and output you want. It's difficult to tell from your post what you're trying to do. Are you saying you want to go from the number 42 to "\x42"? – BrenBarn Jul 19 '12 at 00:11
  • If what you want is `'\x42'`, then that's going to be printed as `'B'`, because they're the same thing. – MRAB Jul 19 '12 at 00:44
  • 1
    It is only a part of mechanism where I have to create strings representing signs I believe that below code is a solution. my_data = str("0a") # example (chr(92) + "x" + my_data).encode("utf8").decode('unicode-escape') – user1536379 Jul 19 '12 at 01:40

4 Answers4

2

Oy vey...

>>> b'\\x42'.decode('unicode-escape')
'B'
Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

I had a hard time reading the question, but I believe this is what you were trying to do.

>>> aString = str('\\') + str(42)
>>> print aString
\42

if you need the x after the \

>>> aString = str('\\') + 'x' + str(42)
>>> print aString
\x42
Wulfram
  • 3,292
  • 2
  • 15
  • 11
0

Note that in ascii(bytes([255])).replace(r"'b\", ""), the \ after b escaped the following ", which cause the SyntaxError.

Try escaping \ :

>>>ascii(bytes([255])).replace(r"'b\\", "")
satoru
  • 31,822
  • 31
  • 91
  • 141
  • You posted the same code again. Did you mean to add another backslash? – BrenBarn Jul 19 '12 at 00:45
  • I know but putting double back slash produce some strange results. I think that (chr(92) + "x" + my_data).encode("utf8").decode('unicode-escape') should solve my issue. – user1536379 Jul 19 '12 at 01:44
0
>>> chr(92)
'\\'

The result shows 2 slashes, but the string really contains only one. That's just the way the strings are displayed. If you print it you'll see what it really contains.

>>> r"'b\"
SyntaxError: EOL while scanning string literal

Raw strings are not allowed to end with a \ so that \ can be used to escape a quote in the middle of the string. Not sure I agree with that aspect of Python's design, but more information can be found at Why can't Python's raw string literals end with a single backslash?

>>> "\x"
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence

You started an escape sequence that would identify a character by its hex number, but didn't follow it with a hex number.

Following up on point #1, perhaps this explains your confusion:

>>> chr(92) + "x" + str(42)
'\\x42'
>>> print(chr(92) + "x" + str(42))
\x42
Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Re `r"'b\"`, a raw string can't end with a single backslash because the backslash 'escapes' the `"`, although it's also included in the string, so `r"'b\""` is printed as `'b\"`. – MRAB Jul 19 '12 at 00:42
  • @MRAB, thanks. Found a corroborating link and added it to the answer. – Mark Ransom Jul 19 '12 at 01:02