xref: /linux-6.15/include/linux/hex.h (revision 890a3ee3)
1*890a3ee3SAndy Shevchenko /* SPDX-License-Identifier: GPL-2.0 */
2*890a3ee3SAndy Shevchenko #ifndef _LINUX_HEX_H
3*890a3ee3SAndy Shevchenko #define _LINUX_HEX_H
4*890a3ee3SAndy Shevchenko 
5*890a3ee3SAndy Shevchenko #include <linux/types.h>
6*890a3ee3SAndy Shevchenko 
7*890a3ee3SAndy Shevchenko extern const char hex_asc[];
8*890a3ee3SAndy Shevchenko #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
9*890a3ee3SAndy Shevchenko #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
10*890a3ee3SAndy Shevchenko 
hex_byte_pack(char * buf,u8 byte)11*890a3ee3SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
12*890a3ee3SAndy Shevchenko {
13*890a3ee3SAndy Shevchenko 	*buf++ = hex_asc_hi(byte);
14*890a3ee3SAndy Shevchenko 	*buf++ = hex_asc_lo(byte);
15*890a3ee3SAndy Shevchenko 	return buf;
16*890a3ee3SAndy Shevchenko }
17*890a3ee3SAndy Shevchenko 
18*890a3ee3SAndy Shevchenko extern const char hex_asc_upper[];
19*890a3ee3SAndy Shevchenko #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
20*890a3ee3SAndy Shevchenko #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
21*890a3ee3SAndy Shevchenko 
hex_byte_pack_upper(char * buf,u8 byte)22*890a3ee3SAndy Shevchenko static inline char *hex_byte_pack_upper(char *buf, u8 byte)
23*890a3ee3SAndy Shevchenko {
24*890a3ee3SAndy Shevchenko 	*buf++ = hex_asc_upper_hi(byte);
25*890a3ee3SAndy Shevchenko 	*buf++ = hex_asc_upper_lo(byte);
26*890a3ee3SAndy Shevchenko 	return buf;
27*890a3ee3SAndy Shevchenko }
28*890a3ee3SAndy Shevchenko 
29*890a3ee3SAndy Shevchenko extern int hex_to_bin(unsigned char ch);
30*890a3ee3SAndy Shevchenko extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
31*890a3ee3SAndy Shevchenko extern char *bin2hex(char *dst, const void *src, size_t count);
32*890a3ee3SAndy Shevchenko 
33*890a3ee3SAndy Shevchenko bool mac_pton(const char *s, u8 *mac);
34*890a3ee3SAndy Shevchenko 
35*890a3ee3SAndy Shevchenko #endif
36