Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

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 /* ------  functions open for use:  (i.e. non-static) ------------------ 
00003  *
00004  * r_sprintf_long_to_hex  - convert a long to a hex digit string
00005  *
00006  * r_hex_str_to_long      - convert a hex digit string to a long
00007  *
00008  * ---------------------------------------------------------------------
00009 */
00010 
00011 
00012 #include <stdio.h>
00013 #include "r_misc.h"
00014 
00015 /* -- locals */
00016 static int ulong_size ( unsigned long l );
00017 /* -- end locals */
00018 
00019 
00020 /*----------------------------------------------------------------------
00021  * r_hex_str_to_long    - convert hex_digits hex digits to a long
00022  *
00023  * Conver up to 8 hex digits into a single unsigned long integer.
00024  * The input string must consist of hex type characters, without
00025  * a leading 0x.  i.e. '0'->'9', 'a'->'f' and 'A'->'F'.
00026  *
00027  * return the unsigned long integer, or 0 on failure
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  * r_sprintf_long_to_hex    - write hex chars to a string
00064  *
00065  * Convert the low-order number of bytes from the unsigned long into
00066  * a null-terminated string hex characters (strlen() is twice the
00067  * number of converted bytes).
00068  *
00069  * We input the option to pad the string with zeros if the number
00070  * is too small for the requested number of bytes.
00071  *
00072  * return number of hex pairs written (if padding, should equal bytes)
00073  *----------------------------------------------------------------------
00074 */
00075 int
00076 r_sprintf_long_to_hex
00077     (
00078         char          * dest,           /* location of output string     */
00079         unsigned long   lsrc,           /* number to translate           */
00080         int             bytes,          /* total bytes (hex pairs)       */
00081         int             pad             /* pad the result with zeros?    */
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 )       /* 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 }
00115 
00116 /* return number of bytes needed to represent a long - return at least 1 */
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 
 

Powered by Plone

This site conforms to the following standards: