Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
r_misc.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 #include <stdio.h>
00013 #include "r_misc.h"
00014 
00015 
00016 static int ulong_size ( unsigned long l );
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 unsigned long
00031 r_hex_str_to_long ( char * src, int hex_digits )
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 }
00060 
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 int
00076 r_sprintf_long_to_hex
00077     (
00078         char          * dest,           
00079         unsigned long   lsrc,           
00080         int             bytes,          
00081         int             pad             
00082     )
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 )       
00099         ret = size;
00100     else
00101         ret = bytes;
00102 
00103     for ( posn = ret-1; posn >= 0; posn-- )
00104     {
00105         
00106         ub = ( lsrc >> (posn << 3) ) & 0xff;            
00107         *cp++ = hexstring[(ub>>4) & 0xf];               
00108         *cp++ = hexstring[ ub     & 0xf];               
00109     }
00110 
00111     *cp = '\0';
00112 
00113     return ret;
00114 }
00115 
00116 
00117 static int
00118 ulong_size ( unsigned long l )
00119 {
00120     if ( l & 0xff000000 )
00121         return 4;
00122 
00123     if ( l & 0xff0000 )
00124         return 3;
00125 
00126     if ( l & 0xff00 )
00127         return 2;
00128 
00129     return 1;
00130 }
00131