Forum: Ruby check if valid hexadecimal code?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
30ccc4183dc6fbf8f513e1639e03b77b?d=identicon&s=25 Chealsea S. (marikoc)
on 2008-11-02 22:32
How would you check if a string is a valid hexadecimal code?
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2008-11-02 22:58
(Received via mailing list)
On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. <youngliars@gmail.com>
wrote:
> How would you check if a string is a valid hexadecimal code?

irb(main):007:0> hex_pattern = /^[[:xdigit:]]+$/
=> /^[[:xdigit:]]+$/
irb(main):008:0> hex_pattern === "0123456789ABCDEF"
=> true
irb(main):009:0> hex_pattern === "Chunky Bacon"
=> false


--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-11-02 23:04
(Received via mailing list)
On Sun, Nov 2, 2008 at 5:56 PM, Avdi Grimm <avdi@avdi.org> wrote:

> On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. <youngliars@gmail.com> wrote:
> > How would you check if a string is a valid hexadecimal code?
>
> irb(main):007:0> hex_pattern = /^[[:xdigit:]]+$/
> => /^[[:xdigit:]]+$/
> irb(main):008:0> hex_pattern === "0123456789ABCDEF"
> => true
> irb(main):009:0> hex_pattern === "Chunky Bacon"
> => false


I wonder if you should use /\A[[:xdigit:]]+\z/ to match the beginning
and
end of the string instead of the beginning and end of the line.

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

Regards,
Craig
30ccc4183dc6fbf8f513e1639e03b77b?d=identicon&s=25 Chealsea S. (marikoc)
on 2008-11-03 00:18
I tried "hex_pattern" with a string like "asdbgg" and it does not
produce an error, even though "g" isn't A-F 0-9.

I should specify that I'm trying to build a hexadecimal color checker.
I tried the folllowing code but it doesn't work:

    def ishex(hexcolor)
        if (hexcolor.to_s.length == 6) == false
        then
            false
        elsif hexcolor.hex == 0
            false
        else
            true
        end
    end



Thanks for any help.



Avdi Grimm wrote:
> On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. <youngliars@gmail.com>
> wrote:
>> How would you check if a string is a valid hexadecimal code?
>
> irb(main):007:0> hex_pattern = /^[[:xdigit:]]+$/
> => /^[[:xdigit:]]+$/
> irb(main):008:0> hex_pattern === "0123456789ABCDEF"
> => true
> irb(main):009:0> hex_pattern === "Chunky Bacon"
> => false
>
>
> --
> Avdi
>
> Home: http://avdi.org
> Developer Blog: http://avdi.org/devblog/
> Twitter: http://twitter.com/avdi
> Journal: http://avdi.livejournal.com
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2008-11-03 00:25
(Received via mailing list)
On Sun, Nov 2, 2008 at 6:17 PM, Chealsea S. <youngliars@gmail.com>
wrote:
> I tried "hex_pattern" with a string like "asdbgg" and it does not
> produce an error, even though "g" isn't A-F 0-9.

irb(main):012:0> hex_pattern === "asdbggg"
=> false

I can't reproduce that behavior.  What version/platform of Ruby are you
on?

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-03 14:44
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
This topic is locked and can not be replied to.