Text to Hex to Dec conversion 
Author Message
 Text to Hex to Dec conversion

I need to have some text entered and pretend that the number entered
is in hex, then do a decimal conversion to write to a file.  Problem
is, if I printf("%x  %d",variable,variable), I don't get the same
number that I do if I use the Windows Calculator to do the conversion.

here's my sample program:

#include <string.h>
#include <stdio.h>

void main(void)
{
int test_int;
test_int = 0x9950;
printf("%x  %d  %i",test_int,test_int,test_int);

Quote:
}

My output is 9950  -26288  -26288

yet if I set the Windows Calculator to Hex and enter 9950, then click
on Dec to display in decimal format, I get  39248.

How should I do this to get the right answer?

My humble thanks,

Brent



Tue, 08 Dec 1998 03:00:00 GMT  
 Text to Hex to Dec conversion

Quote:

>I need to have some text entered and pretend that the number entered
>is in hex, then do a decimal conversion to write to a file.  Problem
>is, if I printf("%x  %d",variable,variable), I don't get the same
>number that I do if I use the Windows Calculator to do the conversion.

>here's my sample program:

>#include <string.h>
>#include <stdio.h>

>void main(void)
>{
>int test_int;
>test_int = 0x9950;
>printf("%x  %d  %i",test_int,test_int,test_int);
>}

>My output is 9950  -26288  -26288

>yet if I set the Windows Calculator to Hex and enter 9950, then click
>on Dec to display in decimal format, I get  39248.

Your ints are apparently 16 bits, so the maximum range of values is -32768 to
32767.

Either use long ints (and change the printf() formats to %lx, %ld, %li) or, if
the values will always be positive, use an unsigned int, and change the %d to
%u.

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
or: http://vancouver-webpages.com/peter/index.html



Tue, 08 Dec 1998 03:00:00 GMT  
 Text to Hex to Dec conversion

Quote:

>I need to have some text entered and pretend that the number entered
>is in hex, then do a decimal conversion to write to a file.  Problem
>is, if I printf("%x  %d",variable,variable), I don't get the same
>number that I do if I use the Windows Calculator to do the conversion.

>here's my sample program:

>#include <string.h>
>#include <stdio.h>

>void main(void)
>{
>int test_int;
>test_int = 0x9950;
>printf("%x  %d  %i",test_int,test_int,test_int);
>}

>My output is 9950  -26288  -26288

>yet if I set the Windows Calculator to Hex and enter 9950, then click
>on Dec to display in decimal format, I get  39248.

>How should I do this to get the right answer?

>My humble thanks,

>Brent

Use %u in your printf format string instead of %d.


Sat, 12 Dec 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help... conversion of hex to dec

2. dec to hex conversions.....more

3. BIN HEX DEC conversions...HELP!!!!

4. String Hex to Dec conversion.

5. Conversion from hex to text + read thread

6. converting hex to dec (from a file)

7. hex to dec - strtol works best!!

8. HEX to DEC IMAL problemo

9. Converting from hex to dec

10. hex to dec function?

11. hex to dec

12. Convert Dec to Hex??

 

 
Powered by phpBB® Forum Software