Converting from hex to dec 
Author Message
 Converting from hex to dec

HI,
Can any one tell me how to convert from hex(charecters) to decimal

THanks



Fri, 06 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
On Mon, 20 Mar 2000 18:42:19 +1100, "A Missaghi"

Quote:
> HI,
> Can any one tell me how to convert from hex(charecters) to decimal

> THanks

There are quite a few ways, it is hard to decide which one without
more information than you have provided.  Assuming you have proper
'\0' terminated strings with hex character digits, strtol() or
strtoul() with a radix parameter of 16 might work.

Jack Klein
--
Home: http://jackklein.home.att.net



Fri, 06 Sep 2002 03:00:00 GMT  
 Converting from hex to dec

Quote:

> HI,
> Can any one tell me how to convert from hex(charecters) to decimal

> THanks

Sure.

Keep a temp integer 'tmp' initialized to zero.
For each character of the array, figure out how far from the last
character of the array it is, call this number e.g. 'd'. If the current
character is a number, convert it to a real integer 'n' (by subtracting
the ASCII value of '0') and calculate 'n*(1 << 4*d)' and add this to
'tmp', otherwise calulate 'n' by subtracting the ASCII value of 'A' and
add 10 and add 'n*(1 << 4*d)' to 'tmp'.

That's all there is to it!

/A

ps: I wrote that in one go. When I read it now, I can't make anything
out of it...

--
# Andreas K?h?ri
# Brought to you from Uppsala, Sweden
# http://hello.to/andkaha
# Remember, an e-mail is as secure as a postcard

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 06 Sep 2002 03:00:00 GMT  
 Converting from hex to dec

Quote:

> HI,
> Can any one tell me how to convert from hex(charecters) to decimal

Look up function strtoul. It may do what you require.

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

int main(void) {
   char *s,*hx = "0xffff";
   unsigned long value;

   errno = 0;
   value = strtoul(hx,&s,16);
   if(errno != ERANGE && *s == '\0')
      printf("hex value \"%s\" = %lu\n",hx,value);
   return 0;
   }

--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html



Fri, 06 Sep 2002 03:00:00 GMT  
 Converting from hex to dec

Quote:


>> HI,
>> Can any one tell me how to convert from hex(charecters) to decimal

>> THanks

>Sure.

>Keep a temp integer 'tmp' initialized to zero.
>For each character of the array, figure out how far from the last
>character of the array it is, call this number e.g. 'd'. If the current
>character is a number, convert it to a real integer 'n' (by subtracting
>the ASCII value of '0') and calculate 'n*(1 << 4*d)' and add this to
>'tmp', otherwise calulate 'n' by subtracting the ASCII value of 'A' and
>add 10 and add 'n*(1 << 4*d)' to 'tmp'.

>That's all there is to it!

Ah ah !

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 06 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
a) Are you referring to converting a string such as "ab43f" to decimal?
b) Actual hex value to decimal?
c) How is the hex value stored?

Fill in the answers and you might receive some decent assistance.  Sounds
like a homework problem.


Quote:
> HI,
> Can any one tell me how to convert from hex(charecters) to decimal

> THanks



Sat, 07 Sep 2002 03:00:00 GMT  
 Converting from hex to dec

Quote:

> Well, Now that some one wants some detail, here it goes:

> my data is stored as a sequence of charecters.
> eg: 35a4c
> I want to convert each one of the charecters in my string into its
> equivalent HEX value, and store it as an integer(or a long??).
> i.e. charecter 3 is integer 3 , and "a" is integer 10.

#include <ctype.h>
#include <string.h>

int convert1char(int c)
{
    static char *xdigs = "0123456789abcdef";

    return isxdigit(c) ? (int)(strchr(xdigs, tolower(c)) - xdigs) : -1;

Quote:
}

Later, Gregory Pietsch


Thu, 12 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
On Mon, 27 Mar 2000 01:04:56 +1000, "A Missaghi"

Quote:

>Well, Now that some one wants some detail, here it goes:

>my data is stored as a sequence of charecters.
>eg: 35a4c
>I want to convert each one of the charecters in my string into its
>equivalent HEX value, and store it as an integer(or a long??).
>i.e. charecter 3 is integer 3 , and "a" is integer 10.

Either do it with a big if statement or look up the strol function,
and apply it to each character in turn, using an approporiate base.

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Thu, 12 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
On Sun, 26 Mar 2000 22:29:03 +0100, Mark McIntyre

Quote:

>On Mon, 27 Mar 2000 01:04:56 +1000, "A Missaghi"

>>Well, Now that some one wants some detail, here it goes:

>>my data is stored as a sequence of charecters.
>>eg: 35a4c
>>I want to convert each one of the charecters in my string into its
>>equivalent HEX value, and store it as an integer(or a long??).
>>i.e. charecter 3 is integer 3 , and "a" is integer 10.

>Either do it with a big if statement or look up the strol function,
>and apply it to each character in turn, using an approporiate base.

typo - that should be strtol()

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Thu, 12 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
Well, Now that some one wants some detail, here it goes:

my data is stored as a sequence of charecters.
eg: 35a4c
I want to convert each one of the charecters in my string into its
equivalent HEX value, and store it as an integer(or a long??).
i.e. charecter 3 is integer 3 , and "a" is integer 10.


Quote:
> a) Are you referring to converting a string such as "ab43f" to decimal?
> b) Actual hex value to decimal?
> c) How is the hex value stored?

> Fill in the answers and you might receive some decent assistance.  Sounds
> like a homework problem.



> > HI,
> > Can any one tell me how to convert from hex(charecters) to decimal

> > THanks



Fri, 13 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
Thanks every one,
strtol() does the job. After further investigation I found out that there is
a function called strtoul() which is usefull for unsigned long.

Also the following algorithm that -hs- provided was cool: (here it is
again.....)
Keep a temp integer 'tmp' initialized to zero.

Quote:
>For each character of the array, figure out how far from the last
>character of the array it is, call this number e.g. 'd'. If the current
>character is a number, convert it to a real integer 'n' (by subtracting
>the ASCII value of '0') and calculate 'n*(1 << 4*d)' and add this to
>'tmp', otherwise calulate 'n' by subtracting the ASCII value of 'A' and
>add 10 and add 'n*(1 << 4*d)' to 'tmp'.



Quote:
> On Sun, 26 Mar 2000 22:29:03 +0100, Mark McIntyre

> >On Mon, 27 Mar 2000 01:04:56 +1000, "A Missaghi"

> >>Well, Now that some one wants some detail, here it goes:

> >>my data is stored as a sequence of charecters.
> >>eg: 35a4c
> >>I want to convert each one of the charecters in my string into its
> >>equivalent HEX value, and store it as an integer(or a long??).
> >>i.e. charecter 3 is integer 3 , and "a" is integer 10.

> >Either do it with a big if statement or look up the strol function,
> >and apply it to each character in turn, using an approporiate base.

> typo - that should be strtol()

> Mark McIntyre

> C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 13 Sep 2002 03:00:00 GMT  
 Converting from hex to dec
A Missaghi a crit dans le message

Quote:
>Thanks every one,
>strtol() does the job. After further investigation I found out that there
is
>a function called strtoul() which is usefull for unsigned long.

>Also the following algorithm that -hs- provided was cool: (here it is
>again.....)
>Keep a temp integer 'tmp' initialized to zero.
>>For each character of the array, figure out how far from the last
>>character of the array it is, call this number e.g. 'd'. If the current
>>character is a number, convert it to a real integer 'n' (by subtracting
>>the ASCII value of '0') and calculate 'n*(1 << 4*d)' and add this to
>>'tmp', otherwise calulate 'n' by subtracting the ASCII value of 'A' and
>>add 10 and add 'n*(1 << 4*d)' to 'tmp'.

I am not sure this algorithm came from me... I am not so good in English...

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 13 Sep 2002 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. converting hex to dec (from a file)

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