Newbie: Printing hex formatted string using printf

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')

···

--
Posted via http://www.ruby-forum.com/.

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

Wes Gamble wrote:

···

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')

--
Posted via http://www.ruby-forum.com/\.

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)}"

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

···

On 21-Mar-06, at 6:03 PM, Wes Gamble wrote:

wg

Wes Gamble wrote:

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')

--
Posted via http://www.ruby-forum.com/\.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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"]

···

On 21-Mar-06, at 6:11 PM, Mike Stok wrote:

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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

If I do

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

I get

  wrong number of arguments (1 for 0)

wg

Matthew Moss wrote:

···

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)}"

--
Posted via http://www.ruby-forum.com/\.

irb:

123.to_s(16)

=> "7b"

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

···

On 21-Mar-06, at 6:22 PM, Wes Gamble wrote:

If I do

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

I get

  wrong number of arguments (1 for 0)

wg

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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

Mike Stok wrote:

···

On 21-Mar-06, at 6:22 PM, Wes Gamble wrote:

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 <mike@stok.ca>
Mike Stok

The "`Stok' disclaimers" apply.

--
Posted via http://www.ruby-forum.com/\.