Author Topic: [Python] Converting ASCII to Hexadecimal  (Read 1515 times)

0 Members and 3 Guests are viewing this topic.

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
[Python] Converting ASCII to Hexadecimal
« 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
« Last Edit: May 31, 2013, 01:43:05 pm by Raavgo »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [Python] Converting ASCII to Binary
« Reply #1 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
~Factionwars

Offline Raavgo

  • Peasant
  • *
  • Posts: 88
  • Cookies: 12
  • On my way from a n00b to a PRO
    • View Profile
Re: [Python] Converting ASCII to Binary
« Reply #2 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?

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [Python] Converting ASCII to Hexadecimal
« Reply #3 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()
Easter egg in all *nix systems: E(){ E|E& };E