Thread: Decimal array to 8 bit binary array conversion

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    4

    Decimal array to 8 bit binary array conversion

    hi
    i have huge decimal elements in array. i want to convert it into 8 bit binary into another array.

    I would appreciate any help!!!!

    Thanks!!!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What do you mean by "huge decimal elements"?
    What do you mean by "8 bit binary"?
    Give an example.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    4
    thanks for your comment.
    for example...
    in my "a" array, data are placed like [0 1 2 3.......... ]
    I want to convert these array elements into 8 bit binary format('0'=00000000) in another array like [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1........]

    thanks

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It's still not entirely clear. What is the type of your "a" array? What about the other array?

    Assuming it contains digit characters ('0', '1', etc.) and you want to convert them to unsigned chars of their respective values (0, 1, etc) :
    Code:
    #include <stdio.h>
    
    void print_bits(unsigned char x) {
        unsigned char m = 0x80;
        for ( ; m; m >>= 1)
            putchar(x & m ? '1' : '0');
        putchar('\n');
    }
    
    int main() {
        char a[] = "0123";
        unsigned char b[4];
    
        for (size_t i = 0; i < 4; i++)
            b[i] = a[i] - '0';
    
        for (size_t i = 0; i < 4; i++)
            print_bits(b[i]);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    4
    thanks for quick response!!!
    both are single dimensional array.

    thanks

  6. #6
    Registered User
    Join Date
    Mar 2017
    Posts
    4
    both array are integer types too.

    thanks algorism!!!

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't understand what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  2. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  3. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  4. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  5. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM

Tags for this Thread