Doxygen Source Code Documentation
r_misc.c File Reference
#include <stdio.h>
#include "r_misc.h"
Go to the source code of this file.
Functions | |
int | ulong_size (unsigned long l) |
unsigned long | r_hex_str_to_long (char *src, int hex_digits) |
int | r_sprintf_long_to_hex (char *dest, unsigned long lsrc, int bytes, int pad) |
Function Documentation
|
Definition at line 31 of file r_misc.c.
00032 { 00033 unsigned long res = 0; 00034 char * cp; 00035 int nib, digs; 00036 00037 if ( hex_digits <= 0 || hex_digits > 8 ) 00038 return 0; 00039 00040 for ( digs = hex_digits, cp = src; digs > 0; digs--, cp++ ) 00041 { 00042 if ( (*cp >= '0') && (*cp <= '9') ) 00043 nib = *cp - '0'; 00044 else if ( (*cp >= 'a') && (*cp <= 'f') ) 00045 nib = *cp - 'a' + 10; 00046 else if ( (*cp >= 'A') && (*cp <= 'F') ) 00047 nib = *cp - 'A' + 10; 00048 else 00049 { 00050 fprintf( stderr, "r_hex_str_to_long: invalid input string <%8s>\n", 00051 src ); 00052 return 0; 00053 } 00054 00055 res = (res << 4) + (nib & 0xf); 00056 } 00057 00058 return res; 00059 } |
|
---------------------------------------------------------------------- sprintf_long_to_hex - write hex chars to a string return number of hex pairs written (should equal bytes) ---------------------------------------------------------------------- Definition at line 77 of file r_misc.c.
00083 { 00084 static char hexstring[] = "0123456789ABCDEF"; 00085 00086 unsigned char ub; 00087 char * cp = dest; 00088 int posn, size, ret; 00089 00090 if ( (bytes <= 0) || (bytes > 4) ) 00091 { 00092 *cp = '\0'; 00093 return 0; 00094 } 00095 00096 size = ulong_size( lsrc ); 00097 00098 if ( (size < bytes) && !pad ) /* use size if we avoid padding */ 00099 ret = size; 00100 else 00101 ret = bytes; 00102 00103 for ( posn = ret-1; posn >= 0; posn-- ) 00104 { 00105 /* write one hex pair for this byte */ 00106 ub = ( lsrc >> (posn << 3) ) & 0xff; /* current ubyte */ 00107 *cp++ = hexstring[(ub>>4) & 0xf]; /* upper nibble */ 00108 *cp++ = hexstring[ ub & 0xf]; /* lower nibble */ 00109 } 00110 00111 *cp = '\0'; 00112 00113 return ret; 00114 } |
|
Definition at line 118 of file r_misc.c. References l. Referenced by r_sprintf_long_to_hex().
|