How to convert character to hexadecimal?

On May 13, 12:45 pm, 7stud – [email protected] wrote:

For example:
–output:–
one line as well as using ‘x’ as a variable name, a character in the

0x20

puts “%#04x” % dec_str
end

–output:–
0x20
0x09
0x43


Posted viahttp://www.ruby-forum.com/.
Thanks 7, i totally misrad what hex does…thats what I get for
goofing off at work 9^)

cheers

#!/usr/bin/env ruby

about?

Title = “Hex Dumper v1.0”
Author = “Aris Kurniawan”
Company = “ShareSec”

read data

string = ARGV[0]

if string = False

if string.nil?
puts “”
puts " HEX DUMPER v1.0 - Hexadecimal Dump File"
puts " Written by:"
puts " Aris Kurniawan at @ShareSec"
puts “”
puts “Usage: ruby hex_dumper.rb [/path/to/file]”
exit 1

if string = True

else

file path

sources = File.read(string)
charset = sources

array length

len = charset.length
max_val = len-1

hex symbol

hexsym_bslash = 92.chr
hexsym_x = “x”

print output

for loop in 0…max_val
hex_dump = charset[loop].unpack(‘H*’)[0]
print hexsym_bslash+hexsym_x+hex_dump
end

newline

print “\n”
end

EOF

###############################################################
Demos:

root@kali:/home/pentest# cd Desktop
root@kali:/home/pentest/Desktop# touch test.file
root@kali:/home/pentest/Desktop# echo “hello ruby forum :)” >> test.file
root@kali:/home/pentest/Desktop# cat test.file
hello ruby forum :slight_smile:
root@kali:/home/pentest/Desktop# ruby hex_dumper.rb
/home/pentest/Desktop/test.file
\x68\x65\x6c\x6c\x6f\x20\x72\x75\x62\x79\x20\x66\x6f\x72\x75\x6d\x20\x3a\x29\x0a
root@kali:/home/pentest/Desktop# irb
irb(main):001:0> puts
“\x68\x65\x6c\x6c\x6f\x20\x72\x75\x62\x79\x20\x66\x6f\x72\x75\x6d\x20\x3a\x29\x0a”
hello ruby forum :slight_smile:
=> nil
irb(main):002:0> exit 1
root@kali:/home/pentest/Desktop# ruby hex_dumper.rb

  HEX DUMPER v1.0 - Hexadecimal Dump File
               Written by:
      Aris Kurniawan at @ShareSec

Usage: ruby hex_dumper.rb [/path/to/file]
root@kali:/home/pentest/Desktop#

thax all :slight_smile: