lkml.org 
[lkml]   [2010]   [Apr]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH 01/10] lib: introduce common method to convert hex digits
On Fri, 30 Apr 2010 12:34:00 +0300
Andy Shevchenko <ext-andriy.shevchenko@nokia.com> wrote:

> /**
> + * hex_to_bin - convert a hex digit to its real value
> + * @ch: ascii character represents hex digit
> + *
> + * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
> + * input.
> + */
> +int hex_to_bin(char ch)
> +{
> + if ((ch >= 'a') && (ch <= 'f'))
> + return ch - 'a' + 10;
> + if ((ch >= '0') && (ch <= '9'))
> + return ch - '0';
> + if ((ch >= 'A') && (ch <= 'F'))
> + return ch - 'A' + 10;
> + return -1;
> +}
> +EXPORT_SYMBOL(hex_to_bin);

I had to fiddle with it:

- use tolower(), saving 3 bytes!

- test the more common case first - it's quicker.

diff -puN lib/hexdump.c~lib-introduce-common-method-to-convert-hex-digits-fix lib/hexdump.c
--- a/lib/hexdump.c~lib-introduce-common-method-to-convert-hex-digits-fix
+++ a/lib/hexdump.c
@@ -24,12 +24,11 @@ EXPORT_SYMBOL(hex_asc);
*/
int hex_to_bin(char ch)
{
- if ((ch >= 'a') && (ch <= 'f'))
- return ch - 'a' + 10;
+ ch = tolower(ch);
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
- if ((ch >= 'A') && (ch <= 'F'))
- return ch - 'A' + 10;
+ if ((ch >= 'a') && (ch <= 'f'))
+ return ch - 'a' + 10;
return -1;
}
EXPORT_SYMBOL(hex_to_bin);
_

Yielding

int hex_to_bin(char ch)
{
ch = tolower(ch);
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
}


\
 
 \ /
  Last update: 2010-04-30 21:11    [from the cache]
©2003-2014 Jasper Spaans. Advertise on this site