hex characters to int? 
Author Message
 hex characters to int?

   i have a bunch of characters input from a text file. if this string of
   charecters represent a hex number eg "0xbd45e", is there a function to convert
   this string into an appropriate integer?


   please respond by email,

(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead

Sun, 22 Apr 2001 03:00:00 GMT  
 hex characters to int?


> > hi,
> > i have a bunch of characters input from a text file. if this string of
> > charecters represent a hex number eg "0xbd45e", is there a function to convert
> > this string into an appropriate integer?
> Here how to do it:

> * Read a hex value from your file and put it in a character string (char).
> * Put the string value (from the second character into a integer with the
> function:
>         sscanf( myCharacterString + 2, "%x", myInteger );

> If you look at the variable myInteger, you should have the converted decimal
> value.

You did not specify the type for myInteger. It the type is a pointer to an int then
sscanf()may work. If myInteger is type int then the correct statement is:
   sscanf(myCharacterString+2,"%x", (unsigned *)&myInteger);

The danger is using sscanf() to convert a string of hex. The string might
represent a value that overflows the range of an int. scanf() will not detect
this and may yield a incorrect value. For example, the poster gave an
example of a string, "0xbd45e". This string cannot be represented as an
int on my system thus the value sscanf() assigns the int would be incorrect.

For this reason, I suggest you use strtol(). It can detect if a long overflow
occurs and it can be used to detect an int overflow. Run the example
code below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <string.h>

int main(void) {
   char snum[16];
   long lnum;
   char *s;
   int i,inum;

   for(i = 0; i < 4;i++) {
      if(i == 0) sprintf(snum,"%#x",(unsigned)-10);
      else  if(i == 1) strcpy(snum,"0xbd45e");
      else if(i == 2) strcpy(snum,"0xgge");
      else strcpy(snum,"0xfffffffff");
      if(1 == sscanf(snum + 2,"%x",(unsigned *)&inum)) {
         printf("Using sscanf()\n  \"%s\" = %d\n",snum,inum);
         printf("  inum = %d\n",inum);
      else printf("Using sscanf()\n"
                  "  \"%s\" = No conversion made\n",snum);
      errno = 0;
      puts("\nUsing strtol()");
      lnum = strtol(snum,&s,16);
      if(errno == ERANGE || s == snum)
         printf("  \"%s\" Outside the range of a long or"
              " No conversion was made\n",snum);
      else  {
         if(lnum > UINT_MAX) {
            puts("  Value outside range of int");
            printf("  As a long \"%s\" = %ld\n",snum,lnum);
            printf("  lnum = %ld\n",lnum);
         else {
            inum = (int)lnum;
            printf("  \"%s\" = %d\n",snum,inum);
            printf("  inum = %d\n",inum);
   return 0;
Al Bowers
Tampa, FL


Sat, 28 Apr 2001 03:00:00 GMT  
 [ 4 post ] 

 Relevant Pages 

1. Hex Character Constants in Strings

2. Turning hex values in a char array to character equivilants

3. Convert Hex String to actual Characters?

4. newline character as a delimiter for hex records

5. Adding HEX ff to end of character string???

6. HEX semigraphic characters

7. HELP: Character to Hex conversion

8. HELP !! Converting non printable characters to HEX

9. converting int to Hex

10. Convert from int to hex

11. hex string to int

12. hex string 2 int


Powered by phpBB® Forum Software