This is str_hexbin.c in view mode; [Download] [Up]
/******************************************************************************
*H* str_hexbin.c {Hex string value <-> Binary string value } V1.0, 20-APR-91 *
*C* 1.0 20-APR-91 Initial Version --MDM *
******************************************************************************/
#include <string.h> /* Standard C string operator function prototypes */
/******************************************************************************
* FUNCTION: STR_HexToBinary *
* This function converts a formated Hex value to a formated binary value. *
* The hex string is scanned from left to right translating each character in *
* 0-F range by appending the appropriate 4 digit binary sequence to the *
* binary output string. The binary output string is expected to be have *
* enough space to contain the results. *
******************************************************************************/
void STR_HexToBinary
(/* Arguments */
char *hexstr, /*I Pointer to hex value input string */
char *binstr) /*I Pointer to binary value output string */
{/* BEGIN STR_HexToBinary */
*binstr = 0;
while (*hexstr)
{
switch (*hexstr)
{
case '0': strcat(binstr, "0000"); break;
case '1': strcat(binstr, "0001"); break;
case '2': strcat(binstr, "0010"); break;
case '3': strcat(binstr, "0011"); break;
case '4': strcat(binstr, "0100"); break;
case '5': strcat(binstr, "0101"); break;
case '6': strcat(binstr, "0110"); break;
case '7': strcat(binstr, "0111"); break;
case '8': strcat(binstr, "1000"); break;
case '9': strcat(binstr, "1001"); break;
case 'a':
case 'A': strcat(binstr, "1010"); break;
case 'b':
case 'B': strcat(binstr, "1011"); break;
case 'c':
case 'C': strcat(binstr, "1100"); break;
case 'd':
case 'D': strcat(binstr, "1101"); break;
case 'e':
case 'E': strcat(binstr, "1110"); break;
case 'f':
case 'F': strcat(binstr, "1111"); break;
}
hexstr++;
}/*while*/
}/* END STR_HexToBinary */
/******************************************************************************
* FUNCTION: STR_BinaryToHex *
* This function converts a formated Binary value to a formated Hex value. *
* The binary string is scanned from right to left translating each group of *
* four binary digits to an appropriate hex digit. The hex digits are placed *
* in the output string in reverse order. Thus the resultant hex output *
* string is reversed prior to returning to the caller. The hex output string *
* is expected to have enough space to contain the results. *
******************************************************************************/
void STR_BinaryToHex
(/* Arguments */
char *binstr, /*I Pointer to binary value input string */
char *hexstr) /*I Pointer to octal value output string */
{ /* Local Variables */
int binidx; /* current parsing position in binary input string */
char *hexdgt; /* Sequential set of hex digit characters */
int hexidx; /* current output position hex string */
char hexval; /* current value of hex digit being formatted */
char power; /* hex digit bit position power of two (1,2,4,8,...) */
/* BEGIN STR_BinaryToHex */
binidx = strlen(binstr) - 1;
power = 1;
hexidx = 0;
hexval = 0;
hexdgt = "0123456789ABCDEF";
while ( binidx >= 0 )
{/* Continue until we reach the start of binary input string */
if ( *(binstr+binidx) == '1' )
/* Add this bits place value to current hex digit value */
hexval += power;
/* increase next bit positions place value for hex digit */
power = power << 1;
if ( power > 8 || binidx == 0 )
{/* We have checked four bits, kick out a hex digit */
*(hexstr+hexidx) = hexdgt[hexval];
hexidx++;
power = 1;
hexval = 0;
}
binidx--;
}
*(hexstr+hexidx) = 0; /* null terminate output string */
/* Now reverse the order of the octal digits so they make sense */
for (binidx = 0; binidx < hexidx/2; binidx++)
{/* Swap chars at respective ends of string, until middle is reached */
hexval = *(hexstr+binidx);
*(hexstr+binidx) = *(hexstr+hexidx-binidx-1);
*(hexstr+hexidx-binidx-1) = hexval;
}
}/* END STR_BinaryToHex */
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.