Commit 903788892ea0fc7fcaf7e8e5fac9a77379fc215b

Authored by Andy Shevchenko
Committed by Linus Torvalds
1 parent db0fd97c27

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: Andy 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: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 2 changed files with 20 additions and 0 deletions Side-by-side Diff

include/linux/kernel.h
... ... @@ -375,6 +375,8 @@
375 375 return buf;
376 376 }
377 377  
  378 +extern int hex_to_bin(char ch);
  379 +
378 380 #ifndef pr_fmt
379 381 #define pr_fmt(fmt) fmt
380 382 #endif
... ... @@ -16,6 +16,24 @@
16 16 EXPORT_SYMBOL(hex_asc);
17 17  
18 18 /**
  19 + * hex_to_bin - convert a hex digit to its real value
  20 + * @ch: ascii character represents hex digit
  21 + *
  22 + * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  23 + * input.
  24 + */
  25 +int hex_to_bin(char ch)
  26 +{
  27 + if ((ch >= '0') && (ch <= '9'))
  28 + return ch - '0';
  29 + ch = tolower(ch);
  30 + if ((ch >= 'a') && (ch <= 'f'))
  31 + return ch - 'a' + 10;
  32 + return -1;
  33 +}
  34 +EXPORT_SYMBOL(hex_to_bin);
  35 +
  36 +/**
19 37 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
20 38 * @buf: data blob to dump
21 39 * @len: number of bytes in the @buf