Summary: Converting ascii hex to pure hex values

Rodney Radford sasrer at unx.sas.com
Sat Nov 10 05:25:19 AEST 1990


In article <302 at cti1.UUCP> mpledger at cti1.UUCP (Mark Pledger) writes:
>I sure caused alot of traffic for this little question.
>
>Thanks to everyone who responded.
>
>I guess I did'nt make myself clear enough on my question though.  I know I
>can use scanf() or cast a char to int (for a single char).  I DID'NT want to
>use scanf() and casting does not work for my question.  The original question
>went something like this:  If you have a character string with the ascii 
>representation of hex values (e.g. s[] = "63", which will be stored as TWO
>hex byte values \x9 & \x9.  I only want to store them as ONE hex byte of \x63.
>Without using scanf() (or even sprintf())  what is the best (fasted for me) way
>to convert a two-digit ascii code to a one digit hex code, so I can put it back
>into the charater string s[], append a null, and write it back out to disk.  I
>currently use atoi() and just write out the int to disk.  I am interested in
>creating (or finding) a routine that will take a character string as the 
>argument, and returning the hex result in the same character string.
>
>Any suggestions?

I think the following routine (and it's test program) will do what you want...

/**********************************************************************/
/* Simple test program for the function atohex() below.               */
/**********************************************************************/

main()
{
    char test_string[] = "3132332e2E2E20697420776F726B7321";

    printf("input ascii string is................ '%s'\n", test_string);
    atohex(test_string);
    printf("output converted string is........... '%s'\n", test_string);
}

/**********************************************************************/
/* Author: Rodney Radford       SAS Institute, Cary, NC 27513         */
/**********************************************************************/
/* The following function atohex() converts the printable hex string, */
/* that is it's first argument, into a new string,  whose length is   */
/* 1/2 the original string's length, of the decoded hex values (???). */
/**********************************************************************/
/* ASSUMPTION: in it's current form, this routine assumes that the    */
/*     input strings length is even. If this not ALWAYS true then you */
/*     should #define NOT_SURE_ABOUT_EVEN_LENGTH.                     */
/* This also assumes the ASCII character set, but can be in either    */
/* upper or lower case.                                               */
/**********************************************************************/

#define ASCTOHEX(c) (((c) > '9') ? (((c)&0x1F)+9) : ((c)-'0'))

atohex(s)
   char *s;
{
   char *source,*dest;

   for (source=dest=s; *source; source+=2,dest++) {
#ifdef NOT_SURE_ABOUT_EVEN_LENGTH
      if (*(source+1) == '\0') break;
#endif
      *dest = (ASCTOHEX(*source)<<4) + ASCTOHEX(*(source+1));
      }
   *dest = '\0';
}

-- 
Rodney E. Radford        SAS Institute, Inc.        sasrer at unx.sas.com
DG/UX AViiON developer   Box 8000, Cary, NC 27512   (919) 677-8000 x7703



More information about the Comp.lang.c mailing list