[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie: Printing hex formatted string using printf

Wes Gamble

3/21/2006 11:01:00 PM

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-....


8 Answers

Wes Gamble

3/21/2006 11:03:00 PM

0

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-....


matthew.moss.coder

3/21/2006 11:11:00 PM

0

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


Mike Stok

3/21/2006 11:11:00 PM

0


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

> 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

>
> 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-....
>
>

--

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

The "`Stok' disclaimers" apply.






Mike Stok

3/21/2006 11:14:00 PM

0


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
>

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 <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






Wes Gamble

3/21/2006 11:23:00 PM

0

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-....


matthew.moss.coder

3/21/2006 11:31:00 PM

0

irb:

> 123.to_s(16)
=> "7b"


Mike Stok

3/21/2006 11:33:00 PM

0


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>
http://www.stok...

The "`Stok' disclaimers" apply.






Wes Gamble

3/21/2006 11:39:00 PM

0

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>
> http://www.stok...
>
> The "`Stok' disclaimers" apply.


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