hex to dec function? 
Author Message
 hex to dec function?

Hi There,

Is there a function that converts HEX to Dec in ANSI C?

Why re-invent the wheel?

Thanks
Tony



Sat, 08 Aug 1998 03:00:00 GMT  
 hex to dec function?


Quote:
>Hi There,

>Is there a function that converts HEX to Dec in ANSI C?

You could try sscanf.  For example:

main()
{
  char hex[9];
  int dec;

  strcpy(hex,"FFFF");
  sscanf(hex,"%xd",&dec);

Quote:
}

Mike Balfour
--
----------------------------------+--------------------------------
Mike Balfour, Partner             | BS/MS Graduate - ECMP
Overload Engineering              | Case Western Reserve University
"New Ideas for a Brighter Future" | Cleveland, OH


Sun, 09 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:

>>Hi There,

>>Is there a function that converts HEX to Dec in ANSI C?

>You could try sscanf.  For example:

>main()
>{
>  char hex[9];
>  int dec;

>  strcpy(hex,"FFFF");
>  sscanf(hex,"%xd",&dec);

                 ^
What's the purpose of this 'd'?

It's advisable to pass a pointer to unsigned int when using %x in scanf.
Otherwise you violate a "shall" in the standard, invoking undefined
behaviour.

Quote:
>}

BTW, strtol will work for any base in the 2..36 range.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 09 Aug 1998 03:00:00 GMT  
 hex to dec function?


 >

 >
 >>Hi There,
 >>
 >>Is there a function that converts HEX to Dec in ANSI C?
 >>
 >You could try sscanf.  For example:
 >
 >main()
 >{
 >  char hex[9];
 >  int dec;
 >
 >  strcpy(hex,"FFFF");

Of course, someone will change the above to "         FFFFF", thus clobbering
your activation record, since you comitted the hex array to hold only 9
characters.

There is no reason why you can't use   char *hex = "FFFF";  in such a small
example.
--



Sun, 09 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:



> >>Hi There,

> >>Is there a function that converts HEX to Dec in ANSI C?

> >You could try sscanf.  For example:

> >main()
> >{
> >  char hex[9];
> >  int dec;

> >  strcpy(hex,"FFFF");

>Of course, someone will change the above to "         FFFFF", thus clobbering
>your activation record, since you comitted the hex array to hold only 9
>characters.

>There is no reason why you can't use   char *hex = "FFFF";  in such a small
>example.

For such a small example, sscanf("FFFF", "%x", &dec); would have done the
job :-)

Note, however, that there are broken implementations where only the original
example would work, while Kazimir's and mine would segfault!  This
is because string literals _may_ be stored in read-only memory segments
and some old sscanf implementations attempted to write back (actually
to ungetc) the first character that couldn't be converted (the terminating
null in this case).  A few years ago, somebody actually posted a similar
example which crashed on his system and he couldn't understand why.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 10 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:

> Hi There,

> Is there a function that converts HEX to Dec in ANSI C?

> Why re-invent the wheel?

> Thanks
> Tony

I assume you mean convert a character string containing hex digits
('A'-'F','0'-'9') into a decimal integer. If so, use

   unsigned int int_value;
   unsigned long int long_int_value;
   char *string_buffer;

   ...

   sscanf( string_buffer, "%x", int_value );

   sscanf( string_buffer, "%lx", long_int_value );

--
Larry Kearney                   |   "You want fries with that?"
Applied Signal Technology       |



Tue, 11 Aug 1998 03:00:00 GMT  
 hex to dec function?


Quote:
> Hi There,

> Is there a function that converts HEX to Dec in ANSI C?

> Why re-invent the wheel?

> Thanks
> Tony

Try strtol();

Hope this helps.

                                                        Regards,

                                                                        Emil



Wed, 12 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:


>> Hi There,
>> Is there a function that converts HEX to Dec in ANSI C?
>> Why re-invent the wheel?
>> Thanks Tony

>I assume you mean convert a character string containing hex digits
>('A'-'F','0'-'9') into a decimal integer. If so, use
>   unsigned int int_value;
>   unsigned long int long_int_value;
>   char *string_buffer;
>   ...

I hope the ... means 'have a string_buffer = malloc (somesize);' here.
And don't forget to put your hex string there.

Quote:
>   sscanf( string_buffer, "%x", int_value );
>   sscanf( string_buffer, "%lx", long_int_value );

At the risk of stating the obvious, my workstation will probably
dump core because sscanf wants _pointers_ to int_value and
long_int_value, i.e. &int_value.
Please please please _test_compile_ any answers you give before posting.
And run them. Even more so, don't post bogus code.

Thanks for your cooperation.

Bye, Jens
--
SIGSIG -- signature too long (core dumped)



Fri, 14 Aug 1998 03:00:00 GMT  
 hex to dec function?
Or just use my favourite all purpose converter, sscanf().  Something
like this works:

char *hex = "0x11";
int  num;

sscanf(hex,"0x%x",&num);

--> num contains 17 as expected.

And of course you can go the other way too, using it's counterpart
sprintf().

Good luck

Roberto



: > Hi There,
: >
: > Is there a function that converts HEX to Dec in ANSI C?
: >
: > Why re-invent the wheel?
: >
: > Thanks
: > Tony
: >
: >
: >

: Try strtol();

: Hope this helps.

:                                                       Regards,

:                                                                       Emil

--




----------------------------------------------------------------------



Fri, 14 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:

>> Hi There,

>> Is there a function that converts HEX to Dec in ANSI C?

>> Why re-invent the wheel?

>> Thanks
>> Tony

strtol


Fri, 14 Aug 1998 03:00:00 GMT  
 hex to dec function?

 >Why does the following hang my pc when the printf statement is being

Because your PC's so-called OS is not robust enough to protect it self from an
insignificant little programming bug.

 >executed. If I put a breakpoint on the printf, watch and reset,
 >everything seems OK up to that point. The answer appears to be in the
 >above comments, but I do not understand the premise.
 >
 >   char *pref = "0x";
 >   char *suff;
 >   long numb;
 >
 >      scanf("%s",suff);

Suff is an unitialized pointer. It points to no valid storage. You could have
declared suff as a character array,  char suff[20];

Using "%s" in scanf without a numeric restriction, such as "%19s", is so bad,
you might as well use gets().

 >      strcat(pref,suff);

This is incorrect. You are trying to write to a string literal, which is
illegal. Furthermore, you are trying to write _past_ the literal, which is
absolutely taboo.

 >      sscanf(pref,"0x%lx",&numb);

You don't need to scan the 0x prefix. Why not check a reference like the K&R2
if you are not sure?
--



Thu, 20 Aug 1998 03:00:00 GMT  
 hex to dec function?
: >

: [snip]

: > Assuming you have a string representing some hexadecimal quantity and
: > you want to print its equivalent decimal value:
: >
: > #include <stdio.h>
: > #include <stdlib.h>
: >
: > /*
: >  :
: >  :
: >  */
: > {
: >    char *strHexVal = "20";  /* 32 auf decimal */
: >
: >    (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
: > [snip]

: Just a quick sidenote: if you use strtol() in a printf call, use the
: correct format string, also: "%ld\n".

: Later,
:  AriL

OK, OK.. you got me.  I should have commented that I was assuming int
and long were of the same length (a trait not often found on the
braindead iAPX platform :).

The above works on most modern Unices.

Greg
--

University of Alabama in Huntsville
CS Department Systems Support Team



Mon, 31 Aug 1998 03:00:00 GMT  
 hex to dec function?

Quote:


>: >
>: >    (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
>: > [snip]

>: Just a quick sidenote: if you use strtol() in a printf call, use the
>: correct format string, also: "%ld\n".

>OK, OK.. you got me.  I should have commented that I was assuming int
>and long were of the same length (a trait not often found on the
>braindead iAPX platform :).

The assumption is both brain dead and unnecessary.

Quote:
>The above works on most modern Unices.

Who cares?  Broken code which happens to "work" on your machine is even
worse than broken code which doesn't work.

BTW, I'm typing this article on a modern Unix system where int and long
have different sizes.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 31 Aug 1998 03:00:00 GMT  
 hex to dec function?


: : >

: : [snip]

: : > Assuming you have a string representing some hexadecimal quantity and
: : > you want to print its equivalent decimal value:
: : >
: : > #include <stdio.h>
: : > #include <stdlib.h>
: : >
: : > /*
: : >  :
: : >  :
: : >  */
: : > {
: : >    char *strHexVal = "20";  /* 32 auf decimal */
: : >
: : >    (void) printf("%d\n", strtol(strHexVal, (char **) NULL, 10));
: : > [snip]

: : Just a quick sidenote: if you use strtol() in a printf call, use the
: : correct format string, also: "%ld\n".

: : Later,
: :  AriL

: OK, OK.. you got me.  I should have commented that I was assuming int
: and long were of the same length (a trait not often found on the
: braindead iAPX platform :).

: The above works on most modern Unices.

It didn't work on mine and why should it?  How does strtol know that "20" is
base 16?  The following, Is what I think you were trying to convey:

    char *strHexVal = "0x20";  /* 32 auf decimal */

    printf ("%d\n", strtol (strHexVal, (char **) NULL, 16));
                                                       ^^^
                                                       not 10

The following works too:

    char *strHexVal = "20";

    printf("%d\n",((((atol(strHexVal)) / 10) * 16) + ((atol(strHexVal) % 10))));

regards
--
John J. Rushford              
Westminster, Colorado___/\_/\_



Tue, 01 Sep 1998 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. DEC FORTRAN/DEC C character function

2. converting hex to dec (from a file)

3. hex to dec - strtol works best!!

4. HEX to DEC IMAL problemo

5. Converting from hex to dec

6. Help... conversion of hex to dec

7. Text to Hex to Dec conversion

8. dec to hex conversions.....more

9. hex to dec

10. Convert Dec to Hex??

11. BIN HEX DEC conversions...HELP!!!!

12. #define DEC or HEX?

 

 
Powered by phpBB® Forum Software