Newbie: Printing hex formatted string using printf

W

Wes Gamble

I am trying to print the hex representation of a string using printf.

$stderr.print "Magic number is: #{magic_number}.printf('%X')\n"

Why does this not work?

The output is:

Magic number is: %PDF-1.3.printf('%X')
 
W

Wes Gamble

In irb, I try to figure out why this doesn't work:

irb(main):001:0> x=3
=> 3
irb(main):002:0> 3.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
from (irb):2
irb(main):003:0> x.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
from (irb):3
irb(main):004:0>

Why does printf act like a private method here?

wg
 
M

Matthew Moss

Why does printf act like a private method here?

printf is not a method on Fixnum. The error is confusing (except that
the first word is "NoMethodError").

If you want to use printf, just do:

$stderr.printf "Magic number is: %X\n", magic_number

Or, more ruby-ish:

puts "Magic number is: #{magic_number.to_s(16)}"
 
M

Mike Stok

In irb, I try to figure out why this doesn't work:

irb(main):001:0> x=3
=> 3
irb(main):002:0> 3.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
from (irb):2
irb(main):003:0> x.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
from (irb):3
irb(main):004:0>

Why does printf act like a private method here?

Because printf is a method of Kernel

ratdog:~ mike$ irb
irb(main):001:0> x = 3
=> 3
irb(main):002:0> Kernel.printf('%X', x)
3=> nil
irb(main):003:0> 3.methods.include?('printf')
=> false
irb(main):004:0> Kernel.methods.include?('printf')
=> true

Does this help?

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
M

Mike Stok

Because printf is a method of Kernel

ratdog:~ mike$ irb
irb(main):001:0> x = 3
=> 3
irb(main):002:0> Kernel.printf('%X', x)
3=> nil
irb(main):003:0> 3.methods.include?('printf')
=> false
irb(main):004:0> Kernel.methods.include?('printf')
=> true

And for a Fixnum printf is indeed private:

ratdog:~ mike$ irb
irb(main):001:0> 3.private_methods
=> ["select", "lambda", "local_variables", "chomp", "raise", "sub!",
"Array", "print", "trap", "method_missing", "format", "exit!",
"at_exit", "readlines", "set_trace_func", "autoload", "system",
"getc", "initialize_copy", "split", "fail", "putc", "gsub!",
"iterator?", "catch", "p", "remove_instance_variable", "sleep",
"sub", "syscall", "callcc", "Integer", "fork", "srand",
"irb_binding", "singleton_method_removed", "caller", "chop!", "puts",
"scan", "autoload?", "binding", "block_given?", "throw", "warn", "`",
"gsub", "loop", "Float", "open", "singleton_method_undefined",
"rand", "exit", "chomp!", "gets", "trace_var", "load", "exec",
"global_variables", "proc", "initialize", "chop", "printf", "String",
"test", "sprintf", "abort", "readline", "untrace_var", "require",
"eval"]


--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
W

Wes Gamble

If I do

puts "Magic number is: #{magic_number.to_s(16)}"

I get

wrong number of arguments (1 for 0)

wg
 
M

Mike Stok

If I do

puts "Magic number is: #{magic_number.to_s(16)}"

I get

wrong number of arguments (1 for 0)

wg

What is in magic_number? If it's something other than an integer-y
number then the conversion base is not expected e.g.

ratdog:~ mike$ /usr/bin/irb
irb(main):001:0> magic_number = 3
=> 3
irb(main):002:0> puts "Magic number is: #{magic_number.to_s(16)}"
Magic number is: 3
=> nil
irb(main):003:0> "foo".to_s(16)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):3:in `to_s'
from (irb):3
irb(main):004:0> 123.4.to_s(16)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):4:in `to_s'
from (irb):4

Mike


--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
W

Wes Gamble

magic_number is a string returned by IO.read or IO.sysread

I wanted it to be raw bytes but couldn't find a way to get them.

Wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top