Convert an int to ASCII character

Straightforward way: char digits[] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ }; char aChar = digits[i]; Safer way: char aChar=”0″ + i; Generic way: itoa(i, …) Handy way: sprintf(myString, “%d”, i) C++ way: (taken from Dave18 answer) std::ostringstream oss; oss << 6; Boss way: Joe, write me an int to char … Read more

Convert binary to ASCII and vice versa

For ASCII characters in the range [ -~] on Python 2: >>> import binascii >>> bin(int(binascii.hexlify(‘hello’), 16)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> binascii.unhexlify(‘%x’ % n) ‘hello’ In Python 3.2+: >>> bin(int.from_bytes(‘hello’.encode(), ‘big’)) ‘0b110100001100101011011000110110001101111’ In reverse: >>> n = int(‘0b110100001100101011011000110110001101111’, 2) >>> n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() ‘hello’ To support all … Read more

Python Unicode Encode Error

Likely, your problem is that you parsed it okay, and now you’re trying to print the contents of the XML and you can’t because theres some foreign Unicode characters. Try to encode your unicode string as ascii first: unicodeData.encode(‘ascii’, ‘ignore’) the ‘ignore’ part will tell it to just skip those characters. From the python docs: … Read more