IsHex() ??

  • Thread starter Thread starter Tremendo
  • Start date Start date
T

Tremendo

bool Char.IsHex(char c)

Is there any function like that?

Char.IsNumber() definitely does not work.

Thank you.
 
In message (e-mail address removed),
Tremendo <[email protected]> Proclaimed from the tallest tower:

:: bool Char.IsHex(char c)
::
:: Is there any function like that?
::
:: Char.IsNumber() definitely does not work.
::
:: Thank you.

Not aware of one, but it would be a pretty easy function to write...
 
In message (e-mail address removed),
Tremendo <[email protected]> Proclaimed from the tallest tower:

:: bool Char.IsHex(char c)
::
:: Is there any function like that?
::
:: Char.IsNumber() definitely does not work.
::
:: Thank you.

Not aware of one, but it would be a pretty easy function to write...

Sure, but I am amazed that, among the hundreds of functions that the framework includes, they forgot
to include one so useful as that.


BTW, is there anything shorter than this?

if (Char.IsDigit(c) || ((Char.ToLower(c) >='a') && (Char.ToLower(c) <='f')))
....


I mean, is there any function like this?
bool Char.InSet(char c,string set_of_case_insensitive_characters)

that I could call like this

if (Char.InSet(c,"0123456789ABCDEF"))
....

Thanks
 
private bool IsHex(char c) {
return Char.IsLetterOrDigit(c) && Char.ToLower(c)<='f';
}

this should work.
Cheers,
Fabrizio
 
bool Char.IsHex(char c)

Is there any function like that?

Char.IsNumber() definitely does not work.

Thank you.

bool System.Uri.IsHexDigit(char) would seem to be what you want.

rossum
 
Something like

if ("0123456789ABCDEF".IndexOf(c) >= 0)...
 
1. It is in general better to use ToLower rather than ToUpper, because of
the German small letter sharp S (Eszett) which translates to double S in
uppercase.

2. Although these days cycles to not matter much, converting the character
to uppercase is an expensive operation, locale dependent, and in this simple
case I think it would be better to add the lowercase letters to the string.

JR
 
JR said:
1. It is in general better to use ToLower rather than ToUpper, because of
the German small letter sharp S (Eszett) which translates to double S in
uppercase.

2. Although these days cycles to not matter much, converting the character
to uppercase is an expensive operation, locale dependent, and in this simple
case I think it would be better to add the lowercase letters to the string.

Converting to lowercase is locale dependent as well. Personally I'd
just use

return (x >= '0' && x <= '9') ||
(x >= 'a' && x <= 'f') ||
(x >= 'A' && x <= 'F');

Simple and effective.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top