0

I would like to add hexvalues to a binary string so that I end up with a binary string that can be transmitted. What I want is: StringToAppend = "5ce7e615ff0000000000010202041f0140009e005d006404084c5ce82215ff1d02000000010202041f013b0097005c005e04777c" (I have this in unhexlified form and want to append it to a string a la StatusStr = chr(0) How do I do this??? This is what i have:

>>> not_macs_buffer=     unhexlify("5ce7e615ff0000000000010202041f0140009e005d006404084c5ce82215ff1d02000000010202041    f013b0097005c005e04777c")
>>> StatusStr = chr(0)
>>> for i in xrange(0,len(not_macs_buffer)):
...     StatusStr +=chr(not_macs_buffer[i])
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: an integer is required
>>> 
4
  • It isn't very clear here what you want to do. Perhaps a smaller example with a step by step bit of code would work better... Commented Nov 14, 2012 at 1:47
  • 2
    are you sure that's your example code? I get: TypeError: Non-hexadecimal digit found when I try to run binascii.unhexlify -- Probably due to the whitespace in there ... Commented Nov 14, 2012 at 1:48
  • 1
    Also, am I missing here or do you just want StatusStr = chr(0) + unhexlify(...)? Commented Nov 14, 2012 at 1:49
  • mgilson - no, i need to string in binary form i.e. "unhexlified" Commented Nov 14, 2012 at 15:25

2 Answers 2

1

What are you transmitting the string to/from? Does it have to be in hex?

The issue seems to be that are you are converting your hex string into a binary string, then in your loop you are attempting to convert a string character into a character, using chr(). This fails because chr() only takes an integer value representing a 256-value ASCII code, not a string.

To fix your problem, just change StatusStr +=chr(not_macs_buffer[i]) to this:

StatusStr += not_macs_buffer[i]

Of course, you could forgo the loop completely.

StatusStr = chr(0) + not_macs_buffer

And if you really did need to convert a list of integers to a string, you could use a list comprehension and then join the list. (I won't give an example since it's not relevant)

EDIT:

If you want to add the null value to your original hex string, you can do this:

StringToAppend = '5ce7e6' # ... snip the real value
StatusStr = hexlify(chr(0)) + StringToAppend 
# or
StatusStr = hexlify('\x00') + StringToAppend 
# or
StatusStr = '0000' + StringToAppend 
Sign up to request clarification or add additional context in comments.

2 Comments

No, I actually want to compile a binary strong like: "000b13AB" = list of values like 0x00 0x0b 0x13 0xab
I think you need to be clear on your terminology. Python stores binary values in a string using escape characters, like so: "\x00\x0B\x13\xAB". What you have listed is a string containing hexadecimal characters, "000B13AB". Hexlify will convert the binary string to hexadecimal characters, unhexlify will convert the hexadecimal characters to a binary string. If you want to add a value to the binary string, just do '\x00' + unhexlify(StringToAppend).
0

Well, Thanks all but what I actually ended up doing to get what I wanted, is:

>>> not_macs_buffer=         unhexlify("5ce7e615ff0000000000010202041f0140009e005d006404084c5ce82215ff1d02000000010202041      f013b0097005c005e04777c")
>>> StatusStr = chr(0)
>>> for i in xrange(0,len(not_macs_buffer)):
...     StatusStr +=chr(ord(not_macs_buffer[i]))

2 Comments

Just so you know, "chr(ord(not_macs_buffer[i]))" can be replaced with just "not_macs_buffer[i]". You're converting a character in a string to an integer and back to a string, which is redundant.
no, that what confused me as well, it gives me an "an integer is required" - error - must be something with unicode or utf-8 or so I assumed...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.