Commit 90378889 authored by Andy Shevchenko's avatar Andy Shevchenko Committed by Linus Torvalds

lib: introduce common method to convert hex digits

hex_to_bin() is a little method which converts hex digit to its actual
value.  There are plenty of places where such functionality is needed.

[akpm@linux-foundation.org: use tolower(), saving 3 bytes, test the more common case first - it's quicker]
[akpm@linux-foundation.org: relocate tolower to make it even faster! (Joe)]
Signed-off-by: default avatarAndy Shevchenko <ext-andriy.shevchenko@nokia.com>
Cc: Tilman Schmidt <tilman@imap.cc>
Cc: Duncan Sands <duncan.sands@free.fr>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Greg Kroah-Hartman <gregkh@suse.de>
Cc: "Richard Russon (FlatCap)" <ldm@flatcap.org>
Cc: John W. Linville <linville@tuxdriver.com>
Cc: Len Brown <lenb@kernel.org>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent db0fd97c
...@@ -375,6 +375,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte) ...@@ -375,6 +375,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
return buf; return buf;
} }
extern int hex_to_bin(char ch);
#ifndef pr_fmt #ifndef pr_fmt
#define pr_fmt(fmt) fmt #define pr_fmt(fmt) fmt
#endif #endif
......
...@@ -15,6 +15,24 @@ ...@@ -15,6 +15,24 @@
const char hex_asc[] = "0123456789abcdef"; const char hex_asc[] = "0123456789abcdef";
EXPORT_SYMBOL(hex_asc); EXPORT_SYMBOL(hex_asc);
/**
* 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 >= '0') && (ch <= '9'))
return ch - '0';
ch = tolower(ch);
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
}
EXPORT_SYMBOL(hex_to_bin);
/** /**
* hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
* @buf: data blob to dump * @buf: data blob to dump
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment