Need help in converting char to binary in c language

This is a discussion on Need help in converting char to binary in c language within the C Programming forums, part of the General Programming Boards category; Hi, can anyone help me on converting a char array to binary ?? For example, i have Code: char MESS[4] ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Need help in converting char to binary in c language

    Hi, can anyone help me on converting a char array to binary ??

    For example, i have

    Code:
    char MESS[4] = {"1A,"};
    How do i convert this char array to binary ??

    I'm currently using C programming . Thanks .

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That depends entirely on what you think you mean by "convert to binary". Of course the data is already stored internally in binary, because quite frankly everything inside your computer is binary.

    So what do you mean?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    I do know that the data stored in binary but what i meant is that I need the data to be shown in binary bits, not in char .

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I do know that the data stored in binary but what i meant is that I need the data to be shown in binary bits, not in char .

    Check the least significant bit of the byte (using bitwise AND or % 2). If it's set, you'll print a '1', otherwise '0'. Shift the byte to the right once. Do this a total of CHAR_BIT times per byte.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Check the least significant bit of the byte (using bitwise AND or % 2). If it's set, you'll print a '1', otherwise '0'. Shift the byte to the right once. Do this a total of CHAR_BIT times per byte.
    Just don't forget to do this backwards, otherwise you'll get the lest significant bit printed first. You could store the bits, and then print them backwards, but it might be cleaner to check the most significant bit and then shift left.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Or print the most significant bit first, then the next one, then the next one etc.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    1. Take each element of your string, which will be a char
    *The char is effectively an int
    2. pass this int to a function
    3. this function will either recursively divide your int by two(recursive method), or you can right shift the bit(bitwise) method. With bitwise method you must know your int size for your platform.
    4. With either method you chose you can either go with a MASK of the bit or use a modulus of %2 to get the remainder. It is this remainder that you will be displaying the bit for.

    5. With a recursive method you will probable print the bit pattern as is.

    6. With a bitwise method you will either store the bit patter in an array and return it, or print it out as well.

    These are just some steps to consider as there are many ways to accomplish this.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    A char is effectively an integer, but it is not effectively an int. And int is generally the word size of the platform (32 bit / 64 bit), where as a char is a single byte.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by sean View Post
    A char is effectively an integer, but it is not effectively an int. And int is generally the word size of the platform (32 bit / 64 bit), where as a char is a single byte.
    No argument there. Yes there will be a slight conversion process, to an int, then when you pass this char to your conversion function.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Hi , thanks for the replies . I will try and work out the program.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It's suspicious that your example contains "1A" - which could mean you want to interpret the string as a hex value. In this case decimal 26. The comma perhaps a delimiter to allow multiple such values.

    Or you want each character to be displayed as '0's and '1's individually.

    Your problem of "convert to binary" should be more clearly defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21