I am trying to use str.encode()
but I get
>>> "hello".encode(hex)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: must be string, not builtin_function_or_method
I have tried a bunch of variations and they seem to all work in Python 2.5.2, so what do I need to do to get them to work in Python 3.1?
Possible Duplicate:
C++ convert hex string to signed integer
I allready searched on google but didn't find any help.So heres my problem:I have strings that allready contains hex code e.g.: string s1 = "5f0066"and i want to convert this string to hex.I need to compare the string with hex code i've read in a binary file.
(Just need the basic idea how to convert the string)5fBest regards and thanks.
Edit: Got the binary file info in the format of unsigned char. SO its 0x5f...WOuld like to have teh string in the same format
I am trying to convert the hexadecimal values of an array into characters of another one. Here is the only way, I could find but not working.
Thank you for your help !
char tmp[] = {0x81, 0x00, 0x00, 0x00, 0x12, 0x05};char new[12];for (int i = 0; i < 6; i++) { printf(" %x", tmp[i]); sprintf(new + i, "%x", tmp[i]);}for (int i = 0; i < 12; i++) { printf(" %c", new[i]);}printf("new: %s\n", new);
Here is the output :
81 0 0 0 12 5 8 0 0 0 1 5new: 800015
So, it lacks some bytes ...
I have a loop that converts each latter from a string to its hex value equivalent but there's an esthetic downside when it comes to the values between 0x0 and 0xF which can be written also as 0x00, 0x02... 0x0F. I need an easy way to add zero in front of each of these values so they will be represented as 01, 03... 0D and not just 1, 3... D.
This is the for loop I'm using at the moment:
for i in range(0, len(text)): HexText = HexText + " " + format(ord(text[i]), "X")
where "text" is a string and "HexText" is a buffer for later use.
i have some troubles with decoding HEX strings , im getting
Traceback (most recent call last):key : aoutput = output.decode("hex")HEX : File "C:\Python27\lib\encodings\hex_codec.py", line 42, in hex_decode output = binascii.a2b_hex(input)TypeError: Odd-length string
I tried some methods to solve it but nothing seems to work. Also when im iterating some print's without decoding thing, for loop crashes at g letter.any clues ?
My Code:key1 = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']key1 = int(key1, 16)for letter in alphabet: print "-------------------" print "key : " + letter print "-------------------" print "HEX : " key2 = int(letter, 16) result = key1 ^ key2 result =hex(result) output = str(result) output = output.decode("hex") print output
Please note that by viewing our site you agree to our use of cookies (see Pribadi for details). You will only see this message once.