PDA

View Full Version : Command needed to convert text to hex



Metacarpal
September 10th, 2007, 08:20 PM
Hello,

Does anyone know a standard unix/linux command to convert a text string to hexidecimal?

I've tried hexdump, but I need a single, unified hex string rather than the broken-up format hexdump gives.

If there is a command to do this without my having to parse hexdump output, that would be great. (I'd also be happy with perl code, though I'm less adept at perl than unix shell.)

Thank you in advance!

[h2o]
September 10th, 2007, 08:24 PM
The problem is that string->hexadecimal can be done in a great number of ways depending on encoding.

I don't know of any standard command, but it should be pretty easy to code in whatever language you prefer. Loop through all characters and then translate to integer which you then display as hexadecimal.

xlinuks
September 10th, 2007, 09:48 PM
If I understand you correctly you could use ghex2 and then save the hex as html, just be sure to save into a separate folder.

Wybiral
September 10th, 2007, 10:04 PM
You could always hack up a small python program for it...



in_string = raw_input("Text: ")
out_string = ""
for c in in_string:
out_string += hex(ord(c)).split("x")[1] + " "
print(out_string)

bashologist
September 10th, 2007, 10:32 PM
This might work, but it definitely looks neat!

hexdump<<<"testing hex conversion" | sed "s/ *//g;s/^.\{7\}//;s/\(..\)\(..\)/\2\1/g" | tr -d "\n"; echo
http://www.nickciske.com/tools/hex.php

nanotube
September 11th, 2007, 04:36 AM
here's another solution in python (sample session shown below)


>>> import binascii
>>> binascii.hexlify('blabla')
'626c61626c61'

if you need to read from a file or from stdin, obviously you'd add that... :)

bogolisk
September 11th, 2007, 05:53 AM
Hello,

Does anyone know a standard unix/linux command to convert a text string to hexidecimal?

I've tried hexdump, but I need a single, unified hex string rather than the broken-up format hexdump gives.

If there is a command to do this without my having to parse hexdump output, that would be great. (I'd also be happy with perl code, though I'm less adept at perl than unix shell.)

Thank you in advance!

Not really a single line but will do until 256 octets (that is 512 hex characters per line)


cat file | xxd -c 256 -ps

The -ps option is for plain style. I believe that's what you're after.

Metacarpal
September 14th, 2007, 12:09 AM
Not really a single line but will do until 256 octets (that is 512 hex characters per line)


cat file | xxd -c 256 -ps

The -ps option is for plain style. I believe that's what you're after.

That's prefect - Thanks!!!