From: Brian Candler Date: 2008-11-03T22:43:06+09:00 Subject: Re: check if valid hexadecimal code? Craig Demyanovich wrote: > Another options is to use String#hex method and check the return value, > though I find it weird that it returns 0 on error, since "0x0".hex also > returns 0 It just stops at the first non-hex character. irb(main):002:0> "123zzz".hex => 291 But you could do this: irb(main):003:0> str="123" => "123" irb(main):004:0> Integer("0x#{str}") => 291 irb(main):005:0> str="123zzz" => "123zzz" irb(main):006:0> Integer("0x#{str}") ArgumentError: invalid value for Integer: "0x123zzz" from (irb):6:in `Integer' from (irb):6 from :0 -- Posted via http://www.ruby-forum.com/.