String to Hexadecimal Conversion

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

String to Hexadecimal Conversion

Jain, Punit

Hi All,

 

How can I check whether a string is a hexadecimal number or not? E.g. “0x123” is a hex string, but “123” and “xyz” are not.

 

Thanks,

Punit

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Dirk Laurie-2
2014-10-27 17:54 GMT+02:00 Jain, Punit <[hidden email]>:

> How can I check whether a string is a hexadecimal number or not? E.g.
> “0x123” is a hex string, but “123” and “xyz” are not.

str:match("0x[0-9a-f]+")

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Choonster TheMage
In reply to this post by Jain, Punit
On 28 October 2014 02:54, Jain, Punit <[hidden email]> wrote:

Hi All,

 

How can I check whether a string is a hexadecimal number or not? E.g. “0x123” is a hex string, but “123” and “xyz” are not.

 

Thanks,

Punit


Use the %x character class:

str:match("^0x%x+$")
Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Oliver Kroth
In reply to this post by Jain, Punit
Try to find a 0, an x, and at least one hexadecimal:

str:find("^0x%x+$")

wil return 1 when OK, nil if not
(Avoids the string copy that string:match does)

--
Oliver

Am 27.10.2014 um 16:54 schrieb Jain, Punit:

Hi All,

 

How can I check whether a string is a hexadecimal number or not? E.g. “0x123” is a hex string, but “123” and “xyz” are not.

 

Thanks,

Punit


Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Enrico Colombini
In reply to this post by Choonster TheMage
On 28 October 2014 02:54, Jain, Punit <[hidden email]> wrote:
> str:match("^0x%x+$")

Also, it could be uppercase:

   str:lower():match("^0x%x+$")

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Luiz Henrique de Figueiredo
In reply to this post by Jain, Punit
> How can I check whether a string is a hexadecimal number or not? E.g. "0x123" is a hex string, but "123" and "xyz" are not.

If you just want to convert the string to a number, tonumber does that
because it understands the "0x" prefix.

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Tom N Harris
In reply to this post by Enrico Colombini
On Monday, October 27, 2014 05:15:10 PM Enrico Colombini wrote:
> Also, it could be uppercase:
>
>    str:lower():match("^0x%x+$")

str:match("^%s*0[Xx]%x+%s*$")

--
tom <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

RE: String to Hexadecimal Conversion

Leinen, Rick
> str:match("^%s*0[Xx]%x+%s*$")

What library is everyone using?  I'm having trouble finding information on all this syntax, etc.  The standard string library typically uses string.xxx.

Rick

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Frederico Cadete
>> str:match("^%s*0[Xx]%x+%s*$")
>
>What library is everyone using?  I'm having trouble finding information on all this syntax, etc.  The standard string library typically uses string.xxx.

It's not a string library, it's a method in the string type (the
variable is called "str" in that example). The colon syntax is a
shortcut for OO-style programming, see here for example:
http://www.lua.org/pil/16.html

On Mon, Oct 27, 2014 at 7:04 PM, Leinen, Rick <[hidden email]> wrote:
>> str:match("^%s*0[Xx]%x+%s*$")
>
> What library is everyone using?  I'm having trouble finding information on all this syntax, etc.  The standard string library typically uses string.xxx.
>
> Rick
>

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Javier Guerra Giraldez
In reply to this post by Leinen, Rick
On Mon, Oct 27, 2014 at 1:04 PM, Leinen, Rick <[hidden email]> wrote:
> What library is everyone using?  I'm having trouble finding information on all this syntax, etc.  The standard string library typically uses string.xxx.


all strings have a metatable with an __index=string, so any function
of the form "string.xxx(s, ...)" can also be called like "s:xxx(...)"

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Sean Conner
In reply to this post by Dirk Laurie-2
It was thus said that the Great Dirk Laurie once stated:
> 2014-10-27 17:54 GMT+02:00 Jain, Punit <[hidden email]>:
>
> > How can I check whether a string is a hexadecimal number or not? E.g.
> > “0x123” is a hex string, but “123” and “xyz” are not.
>
> str:match("0x[0-9a-f]+")

  And the obligatory LPeg version:

lpeg = require "lpeg"

number = lpeg.P"0" * lpeg.S"Xx" * lpeg.R("09","AF","af")^1 * lpeg.Cc("hex")
       + lpeg.R"09"^1 * lpeg.Cc("integer")

print(number:match "0x123")
print(number:match "123")

  -spc

Reply | Threaded
Open this post in threaded view
|

Re: String to Hexadecimal Conversion

Aapo Talvensaari
In reply to this post by Jain, Punit

On Oct 27, 2014 5:55 PM, "Jain, Punit" <[hidden email]> wrote:
> How can I check whether a string is a hexadecimal number or not? E.g. “0x123” is a hex string, but “123” and “xyz” are not.

How about:

if (("0x123"):find("0x", 1, true)) == 1 and tonumber("0x123") then ...

Reply | Threaded
Open this post in threaded view
|

RE: String to Hexadecimal Conversion

Jain, Punit

Thank you all for the response. Now, I have multiple options to choose from J.

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Aapo Talvensaari
Sent: Tuesday, October 28, 2014 11:20 AM
To: Lua mailing list
Subject: Re: String to Hexadecimal Conversion

 

On Oct 27, 2014 5:55 PM, "Jain, Punit" <[hidden email]> wrote:
> How can I check whether a string is a hexadecimal number or not? E.g. “0x123” is a hex string, but “123” and “xyz” are not.

How about:

if (("0x123"):find("0x", 1, true)) == 1 and tonumber("0x123") then ...