Forum: Ruby image hexcode

Posted by Adriana Mikolaskova (mikolaskova)
on 2012-11-03 22:16
Attachment: hex1.jpeg (622 KB)
Hello,

how can I display an image as hexcode? I want to have the same Hex-Code,
that I get when I open the image in a Hex-Editor...

Regards

Adriana
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2012-11-03 22:54
(Received via mailing list)
On Sat, Nov 3, 2012 at 10:16 PM, Adriana Mikolaskova
<lists@ruby-forum.com> wrote:
> Hello,
>
> how can I display an image as hexcode? I want to have the same Hex-Code,
> that I get when I open the image in a Hex-Editor...

If you can read the file as strings, you can show each byte of the
string as hex like this:

1.9.2p290 :005 > " abc".each_byte {|x| puts x.to_s(16)}
20
61
62
63

Jesus.
Posted by Adriana Mikolaskova (mikolaskova)
on 2012-11-03 23:14
thank you very much!
Posted by Brian Candler (candlerb)
on 2012-11-03 23:21
Adriana Mikolaskova wrote in post #1082665:
> Hello,
>
> how can I display an image as hexcode? I want to have the same Hex-Code,
> that I get when I open the image in a Hex-Editor...

bin = File.read("foo.jpg")
hex = bin.unpack("H*").first
puts hex
Posted by Adriana Mikolaskova (mikolaskova)
on 2012-11-04 10:34
Thank you very much too...this seems to be even easier.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.