Skip to content

Commit

Permalink
lib: introduce common method to convert hex digits
Browse files Browse the repository at this point in the history
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.

[[email protected]: use tolower(), saving 3 bytes, test the more common case first - it's quicker]
[[email protected]: relocate tolower to make it even faster! (Joe)]
Signed-off-by: Andy Shevchenko <[email protected]>
Cc: Tilman Schmidt <[email protected]>
Cc: Duncan Sands <[email protected]>
Cc: Eric W. Biederman <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: "Richard Russon (FlatCap)" <[email protected]>
Cc: John W. Linville <[email protected]>
Cc: Len Brown <[email protected]>
Cc: Joe Perches <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Andy Shevchenko authored and torvalds committed May 25, 2010
1 parent db0fd97 commit 9037888
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
return buf;
}

extern int hex_to_bin(char ch);

#ifndef pr_fmt
#define pr_fmt(fmt) fmt
#endif
Expand Down
18 changes: 18 additions & 0 deletions lib/hexdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
const char hex_asc[] = "0123456789abcdef";
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
* @buf: data blob to dump
Expand Down

0 comments on commit 9037888

Please sign in to comment.