Thread: dec -> bin -> hex in arrays

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    dec -> bin -> hex in arrays

    I have some numbers stored in a character array. Assume length of 3 arrays is:

    dec[2];
    bin[6];
    hex[2];

    Now if I have 34 in the dec array i.e. dec[0]=3 and dec[1]=4, how can I convert this into binary and then into hex to fill the other 2 arrays?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    search -> forums -> google

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one idea.
    >dec[2];
    First make this long enough so you have can use it as a string. To make room for the string terminator, make it at least three char.
    Code:
    char dec[3]
    Then make dec[2] = '\0'

    Now you can use strtol() to convert this to a long:
    Code:
       long num;
       num = strtol(dec, NULL, 10);
    Now to store in a char array as hex, use sprintf():
    Code:
       sprintf(hex,"%X",num);
    There is no library function to convert to binary. To store it in a char array as binary, so you could use a loop to strip each binary digit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Independent-Robert Fisk-Osama
    By a muslim in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:41 PM