EvilZone

Programming and Scripting => Scripting Languages => Topic started by: Raavgo on May 31, 2013, 11:45:20 AM

Title: [Python] Converting ASCII to Hexadecimal
Post by: Raavgo on May 31, 2013, 11:45:20 AM
Hey Guys,
First of all I want to apologize for asking so much.
(well after I found out how PGP decryption works I will distribute it here but until then I will have to ask like the little noob I am ;) )

So here is my question:
I tried to convert a string like this:
SD+ul55JAJ1SuCBF6O03ZTbk+g5DAa+7qCKFszYklCEQn7y5pftRbm6aJSgF4Eys

to its Hexadecimal form which would be: H\xae\x97\x9eI\x00\x9dR\xb8E\xe8\xed7e6\xe4\xfa\x0eC\x01\xaf\xbb\xa8"\x85\xb36$\x94!\x10\x9f\xbc\xb9\xa5\xfbQnn\x9a%(\x05\xe0L\xac

and then store it into a file.
thats how I tried it:
Code: [Select]

import binascii
mes = '''SD+ul55JAJ1SuCBF6O03ZTbk+g5DAa+7qCKFszYklCEQn7y5pftRbm6aJSgF4Eys'''
f = open('PGPHexa.txt', 'w')
f.write(binascii.a2b_base64(mes))

If use it in the command line without the file I get the Hexadecimal form as mentioned above.

But if I store it in a file I get nothing or I get nonsense...
Where is my mistake?

have a nice day
Raavgo
Title: Re: [Python] Converting ASCII to Binary
Post by: Stackprotector on May 31, 2013, 12:51:40 PM
Hey Guys,
First of all I want to apologize for asking so much.
(well after I found out how PGP decryption works I will distribute it here but until then I will have to ask like the little noob I am ;) )

So here is my question:
I tried to convert a string like this:
SD+ul55JAJ1SuCBF6O03ZTbk+g5DAa+7qCKFszYklCEQn7y5pftRbm6aJSgF4Eys

to its binary form which would be: H\xae\x97\x9eI\x00\x9dR\xb8E\xe8\xed7e6\xe4\xfa\x0eC\x01\xaf\xbb\xa8"\x85\xb36$\x94!\x10\x9f\xbc\xb9\xa5\xfbQnn\x9a%(\x05\xe0L\xac (well atleast I think so)

and then store it into a file.
thats how I tried it:
Code: [Select]

import binascii
mes = '''SD+ul55JAJ1SuCBF6O03ZTbk+g5DAa+7qCKFszYklCEQn7y5pftRbm6aJSgF4Eys'''
f = open('PGPBinary.txt', 'w')
f.write(binascii.a2b_base64(mes))

If use it in the command line without the file I get the binary form as mentioned above.

But if I store it in a file I get nothing or I get nonsense...
Where is my mistake?

have a nice day
Raavgo


Quote
H\xae\x97\x9eI\x00\x9dR\xb8E\xe8\xed7e6\xe4\xfa\x0eC\x01\xaf\xbb\xa8"\x85\xb36$\x94!\x10\x9f\xbc\xb9\xa5\xfbQnn\x9a%(\x05\xe0L\xac
This is in Hexadecimal form :)  Hexadecimal is base-16 and binary is base-2
Title: Re: [Python] Converting ASCII to Binary
Post by: Raavgo on May 31, 2013, 01:41:25 PM
Oh silly me :( Well I'll change that but I need to save it into a file any ideas how to do it the right way?
Title: Re: [Python] Converting ASCII to Hexadecimal
Post by: s3my0n on May 31, 2013, 02:21:11 PM
Well "H\xae\x97\x9eI\x00\x9dR\xb8E\xe8\xed7e6\xe4\xfa\x0eC\x01\xaf\xbb\xa8"\x85\xb36$\x94!\x10\x9f\xbc\xb9\xa5\xfbQnn\x9a%(\x05\xe0L\xac" is certainly not hexadecimal, hex digits are from 0 to 9, then from A to F. There are no special chars like %,( or $ ...

As for your problem:
Code: (python) [Select]
string = "SD+ul55JAJ1SuCBF6O03ZTbk+g5DAa+7qCKFszYklCEQn7y5pftRbm6aJSgF4Eys"
hexString = ''.join(['\\'+hex(ord(i)) for i in string])
fp = open("file.txt", "w")
fp.write(hexString)
fp.close()