#include <stdio.h>
#include <stdlib.h>

static char hex_str[20] ;
static char digit_str[17] = "0123456789ABCDEF" ;

char digit_char(unsigned long self)
{
  /* A character representing self. If self is between 0 and 9, it
  ** returns a digit. If between 10 and 15, returns 'A' thru 'F'. */
  return digit_str[self] ;
}

char *my_str_to_hexstr(char *str)
{
 char hs[20] ;
 int count = 0, i = 0 ;
 unsigned long val = strtoul(str, NULL, 10) ;
 do {
   hs[count] = digit_char(val&((unsigned)15)) ;
   val = ((unsigned)val >> (unsigned)4) ;
   count ++ ;
 } while(val != 0) ;
 hs[count] = 'x' ;
 count ++ ;
 hs[count] = '0' ;
 while(count >= 0) {
   hex_str[i] = hs[count] ;
   count -- ;
   i ++ ;
 }
 hex_str[i] = '\0' ;
 return hex_str ;
}

char *str_to_hexstr(char *str)
{
 int count = 0 ;
 unsigned long val = strtoul(str, NULL, 10) ;
 sprintf(hex_str, "0x%x\0", val) ;
 return hex_str ;
}
