convert hex string to decimal

Discussion in 'C Programming' started by Gospill, Mar 27, 2005.

  1. Gospill

    Gospill Guest

    Hi Guru,

    If I have string like "0D76" for example, it is actually a 0x0D76 in hex
    format.
    How can I convert the hex string 0D76 into decimal value ? So that I will
    get decimal value 3446

    Thanks.
     
    Gospill, Mar 27, 2005
    #1
    1. Advertising

  2. Gospill wrote:
    > Hi Guru,
    >
    > If I have string like "0D76" for example, it is actually a 0x0D76 in hex
    > format.
    > How can I convert the hex string 0D76 into decimal value ? So that I will
    > get decimal value 3446


    Please check the FAQ and archives before posting.
    #include <stdlib.h>
    #include <stdio.h>

    int main(void)
    {
    char s[] = "0D76";
    unsigned long x;
    x = strtoul(s, 0, 16);
    printf("The value represented by the string \"%s\" is\n"
    "%lu (decimal)\n" "%#lo (octal)\n" "%#lx (hex)\n",
    s, x, x, x);
    return 0;
    }

    [output]
    The value represented by the string "0D76" is
    3446 (decimal)
    06566 (octal)
    0xd76 (hex)
     
    Martin Ambuhl, Mar 27, 2005
    #2
    1. Advertising

  3. On Sun, 27 Mar 2005 16:15:57 +0800, "Gospill" <>
    wrote:

    >Hi Guru,
    >
    >If I have string like "0D76" for example, it is actually a 0x0D76 in hex


    "0D76" is a string. It occupies five char.

    0x0D76 is not a string. It occupies at most 2 bytes.

    Don't confuse the two and please try not to confuse us by introducing
    completely extraneous data. You either want to process the first or
    the second. I assume form actually you want to deal with the second.

    >format.
    >How can I convert the hex string 0D76 into decimal value ? So that I will
    >get decimal value 3446
    >


    Where is your hex value? Where do you want the result stored?

    If your hex value is already in a variable of integer type, then any
    member of the printf family will do the trick.

    If your hex value is not already in an array of unsigned char, you
    need to get it there (memcpy if in memory, fread if in a file, etc).

    Once you get it there, you need to process each hex digit (also known
    as a nybble) in turn and accumulate the result in an integer of some
    type. Assuming CHAR_BIT is 8 on your system, a function like the
    following should get you started:

    int hex_to_int(unsigned char hex[], int count)
    {
    int sum = 0;
    int i;
    int temp;

    for (i = 0; i < count; i++)
    {
    temp = (hex & 0xf0U) >> 4;
    sum = sum*16 + temp;
    temp = (hex & 0xofU);
    sum = sum*16 + temp;
    }
    return sum;
    }


    <<Remove the del for email>>
     
    Barry Schwarz, Mar 27, 2005
    #3
  4. On Sun, 27 Mar 2005 16:15:57 +0800, Gospill
    <> wrote:

    > If I have string like "0D76" for example, it is actually a 0x0D76 in hex
    > format.


    I presume you mean that it is currently a character string and you want
    it as a binary value?

    > How can I convert the hex string 0D76 into decimal value ? So that I will
    > get decimal value 3446


    Look up strtol() in your reference book.

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

    int main(void)
    {
    char string[] = "0D76";
    long value = strtol(string, NULL, 16);
    printf("value of '%s' is %ld\n", string, value);
    return 0;
    }

    Chris C
     
    Chris Croughton, Mar 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,223
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  3. convert hex to decimal

    , Mar 9, 2006, in forum: Python
    Replies:
    2
    Views:
    2,421
  4. Angus
    Replies:
    6
    Views:
    2,358
    Alf P. Steinbach
    Nov 20, 2007
  5. chirs
    Replies:
    3
    Views:
    168
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page