13 messages
Open this post in threaded view
|

 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
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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]+")
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 In reply to this post by Jain, Punit On 28 October 2014 02:54, Jain, Punit 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,PunitUse the %x character class:str:match("^0x%x+\$")
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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.
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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]>
Open this post in threaded view
|

## RE: String to Hexadecimal Conversion

 > 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
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 >> 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.htmlOn 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 >
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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
Open this post in threaded view
|

## Re: String to Hexadecimal Conversion

 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