converting hex to dec (from a file) 
Author Message
 converting hex to dec (from a file)

hi all,
I am trying to convert a hexidecimal # 'b' into decimal form (11).
I am reading hex characters from a file and trying to convert
them to decimal so I can tell what they are.
Any ideas on how to go about this?
I've tried many ways to convert it, all unsuccessful.

It can easily be converted in a simple
program if the hex character is a char.  (char h = '\xb';)
HOWEVER, I can't seem to read it in as a char and convert it correctly.
ANd all the manipulators I try to use take a char *.

I figure I could do it if I could just prepend that '\x' onto the
front of every hex character I read in, but I can't.

ANy comments would be
great!!!!!!

thanks,
Dan-

-

instruments  phone: x4880  Mail Stop: T426



Sun, 05 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)



Quote:
>hi all,
>I am trying to convert a hexidecimal # 'b' into decimal form (11).

If you have a hex letter in a char variable C (letters '0'-'9' and
'a'-'f' or 'A'-'F') then a straightforward method could be for example
(I is int):

if (C>='0'&&C<='9') I=C-'0';
else if (C>='a'&&C<='f') I=C-'a'+10;
else if (C>='A'&&C<='F') I=C-'A'+10;
else /* it wasn't a hex digit...

or

I=C>='a'?C-'a'+10:C>='A'?C-'A'+10:C-'0';
// this leaves garbage into the I if C didn't contain hex letter

Now you have the numeric value of the hex letter in I.

Timo



Sun, 05 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

Quote:

> hi all,
> I am trying to convert a hexidecimal # 'b' into decimal form (11).
> I am reading hex characters from a file and trying to convert
> them to decimal so I can tell what they are.
> Any ideas on how to go about this?
> I've tried many ways to convert it, all unsuccessful.

Try this...hope it helps.

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

void main( int argc, char **argv )
{
   unsigned char buf[17];
   FILE *inp;
   register i;
   int cnt = 0;
   int  lines = 0;
   long Bytes = 0;

   if ( argc < 2 ) {
      fprintf(stderr, "Usage: hd <file>.\n"); /* hd = hex dump */
      exit(1);
   }

   if ( (inp = fopen( argv[1], "rb")) == NULL ) {
      fprintf(stderr, "Could not open input file %s\n" , argv[1] );
      exit(2);
   }

   memset ( buf, 0x00, sizeof (buf ) );

   cnt = fread( buf, sizeof( char ), 16, inp );
   while ( 1 ) {
      Bytes += cnt;
      if ( cnt > 0 ) {
         lines++;
         fprintf(stdout,
         "%09ld  %02x %02x %02x %02x %02x %02x %02x %02x - %02x %02x %02x %02x
%02x %02x %02x %02x : ", \
         Bytes,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],
\
         buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);

         for ( i = 0; i < 16; i++ ) {
            if ( !isprint(buf[i]) )
               buf[i] = '.';
         }

         fprintf(stdout,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",\
         buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7], \
         buf[8],buf[9],buf[10],buf[11],buf[12],buf[13],buf[14],buf[15]);

         fprintf( stdout, "\n");

         if ( !(lines % 256) )
            fprintf(stdout, "\n" );

         if (feof( inp ) )
            break;

         memset ( buf, 0x00, sizeof (buf ) );
         cnt = fread( buf, sizeof( char ), 16, inp );
      }
      else
         break;
   }
   fclose( inp );

Quote:
}

--
Gary W. Morris
Sr. Software Engineer
USCS International



Sun, 05 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

I'd use a lookup table. Something like this...

char hexlookup[256] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
        0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 10, 11, 12, 13, 14, 15 } ;

--
Andy Knight



Quote:
> hi all,
> I am trying to convert a hexidecimal # 'b' into decimal form (11).
> I am reading hex characters from a file and trying to convert
> them to decimal so I can tell what they are.
> Any ideas on how to go about this?
> I've tried many ways to convert it, all unsuccessful.

> It can easily be converted in a simple
> program if the hex character is a char.  (char h = '\xb';)
> HOWEVER, I can't seem to read it in as a char and convert it correctly.
> ANd all the manipulators I try to use take a char *.

> I figure I could do it if I could just prepend that '\x' onto the
> front of every hex character I read in, but I can't.

> ANy comments would be
> great!!!!!!

> thanks,
> Dan-

> -
> Dan Ascheman

> instruments  phone: x4880  Mail Stop: T426



Mon, 06 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

Quote:

> hi all,
> I am trying to convert a hexidecimal # 'b' into decimal form (11).
> I am reading hex characters from a file and trying to convert
> them to decimal so I can tell what they are.
> Any ideas on how to go about this?
> I've tried many ways to convert it, all unsuccessful.

> It can easily be converted in a simple
> program if the hex character is a char.  (char h = '\xb';)
> HOWEVER, I can't seem to read it in as a char and convert it correctly.
> ANd all the manipulators I try to use take a char *.

> I figure I could do it if I could just prepend that '\x' onto the
> front of every hex character I read in, but I can't.

Try looking at the sscanf() and scanf() functions, you should be able to
read a string and convert it to a number with sscanf().  Once in numeric
format you should be able to do just about anything.
--
********************************************

********************************************
Boys will be boys.
And even that wouldn't matter
if only we could prevent girls from being girls.
                                    Anthony Hope


Mon, 06 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)



Quote:


> >hi all,
> >I am trying to convert a hexidecimal # 'b' into decimal form (11).

He means hexadecimal!

Quote:
> if (C>='0'&&C<='9') I=C-'0';
> else if (C>='a'&&C<='f') I=C-'a'+10;
> else if (C>='A'&&C<='F') I=C-'A'+10;
> else /* it wasn't a hex digit...
> or
> I=C>='a'?C-'a'+10:C>='A'?C-'A'+10:C-'0';
> // this leaves garbage into the I if C didn't contain hex letter

That is incredidibly long winded!
Just use:

   decimal = isdigit(c) ? (c - '0') : (tolower(c) - 'a' + 10);

Adrian

---
WWW    WWW  Adrian Gothard          
WWW    WWW  White Horse Design.    
WWW WW WWW                        


If A equals success, then the formula is A = X + Y + Z, where
X is "work", Y is "play", Z is "Keep your mouth shut" -- Albert Einstein



Fri, 17 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)


Quote:
>Just use:

>   decimal = isdigit(c) ? (c - '0') : (tolower(c) - 'a' + 10);

Wrong.  There is no guarantee that characters in the range a..f are
contiguous in the character set.  The digits, however, are guaranteed
to be contiguous.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sat, 18 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

Quote:





> > >hi all,
> > >I am trying to convert a hexidecimal # 'b' into decimal form (11).

> He means hexadecimal!

> > if (C>='0'&&C<='9') I=C-'0';
> > else if (C>='a'&&C<='f') I=C-'a'+10;
> > else if (C>='A'&&C<='F') I=C-'A'+10;
> > else /* it wasn't a hex digit...
> > or
> > I=C>='a'?C-'a'+10:C>='A'?C-'A'+10:C-'0';
> > // this leaves garbage into the I if C didn't contain hex letter

> That is incredidibly long winded!
> Just use:

>    decimal = isdigit(c) ? (c - '0') : (tolower(c) - 'a' + 10);

Neither of these responses is guaranteed to work.  The C standard does
not require letters to be contiguous -- 'b' - 'a' may not be 1.

Michael M Rubenstein



Sat, 18 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

[...]

Quote:

>> if (C>='0'&&C<='9') I=C-'0';
>> else if (C>='a'&&C<='f') I=C-'a'+10;
>> else if (C>='A'&&C<='F') I=C-'A'+10;
>> else /* it wasn't a hex digit...
>> or
>> I=C>='a'?C-'a'+10:C>='A'?C-'A'+10:C-'0';
>> // this leaves garbage into the I if C didn't contain hex letter

>That is incredidibly long winded!
>Just use:

>   decimal = isdigit(c) ? (c - '0') : (tolower(c) - 'a' + 10);

[...]

A lookup table will still give you the best performance. This one here should
work with any 8-bit character set:

#include <stdio.h>

static  int     digitval[256];

void init_table (void);

int main (void)
{
        char            hexnumber[20], *p;
        unsigned long   val = 0;
        int             digit;

        init_table ();

        puts ("Enter hex number: ");
        gets (hexnumber);  /* hope user won't type more than 19 chars ;) */

        hexnumber[sizeof (unsigned long) << 1] = 0;  /* gee, reading this */
                                                     /* group really made */
                                                     /* me paranoid...    */

        p = hexnumber;

        while (*p) {
                digit = digitval[*p];
                if (-1 == digit) {
                        *p = 0;
                        break;
                }
                val <<= 4;
                val += digit;
                p++;
        }
        printf ("Decimal value of 0x%s is %lu\n", hexnumber, val);

        return 0;

Quote:
}

void init_table (void)
{
        int     i;

        for (i = 0; i < 256; i++)
                digitval[i] = -1;   /* -1 = not a digit */

        for (i = '0'; i <= '9'; i++)
                digitval[i] = i - '0';

        digitval['A'] = 10;
        digitval['B'] = 11;
        digitval['C'] = 12;
        digitval['D'] = 13;
        digitval['E'] = 14;
        digitval['F'] = 15;

        digitval['a'] = 10;
        digitval['b'] = 11;
        digitval['c'] = 12;
        digitval['d'] = 13;
        digitval['e'] = 14;
        digitval['f'] = 15;

Quote:
}      

Cheers,

Rupert

--
Rupert Weber-Henschel
Munich, Germany



Sat, 18 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

Quote:


> >Just use:

> >   decimal = isdigit(c) ? (c - '0') : (tolower(c) - 'a' + 10);

> Wrong.  There is no guarantee that characters in the range a..f are
> contiguous in the character set.  The digits, however, are guaranteed
> to be contiguous.

> Dan

Neither reply seems to have addressed the issue.

char foo[2] = {'\0','\0'};
sprintf(foo, "%c",c);  /* check arg order. */
sscanf(foo, "%x", decimal);   /* and again */

This should be suitably slow.



Sat, 18 Sep 1999 03:00:00 GMT  
 converting hex to dec (from a file)

Quote:





> > >hi all,
> > >I am trying to convert a hexidecimal # 'b' into decimal form (11).

> He means hexadecimal!

Of course he does. Everybody can make a typo fromtime to time.
No need to be picky about it.

Stephan



Sun, 19 Sep 1999 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Converting from hex to dec

2. Convert Dec to Hex??

3. Summary: Converting ascii hex to pure hex values

4. Converting ascii hex values to hex bytes

5. Need Help: Converting from hex to integer and back to hex

6. hex to dec - strtol works best!!

7. HEX to DEC IMAL problemo

8. Help... conversion of hex to dec

9. Text to Hex to Dec conversion

10. hex to dec function?

11. dec to hex conversions.....more

12. hex to dec

 

 
Powered by phpBB® Forum Software