Thread: convert a char to its binary equivalent?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    20

    convert a char to its binary equivalent?

    I'd like to have the user input any two characters and have the binary equivalent of those characters stored in an array.

    I believe I read somewhere that all characters do not have the same number of binary bits, so I will not be able to have a standard size array to do what I want. I may be wrong though.

    Does anybody have any idea how to do this?

    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dre View Post
    I'd like to have the user input any two characters and have the binary equivalent of those characters stored in an array.
    I'm curious as to how you think this differs from
    Code:
    char two_characters[2];

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    ok i'll give it a shot. i think that with the user input i'd have to start by defining say

    char input1;
    char input2;

    and then i'd use scanf to store the user's chars into these char variables and work from there.

    in char two_characters[2] i don't see where the user input comes in, to me it just looks like its creating a character array with two slots.

    not sure though.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well, I'm assuming you want to convert two character nibbles to a binary byte. I think it's probably safe for you to assume that a byte can be represented by an unsigned char which will hold 8 bits.

    This means you just need to convert a string "A5", to a binary equivalent of 165 and store it in an unsigned char variable.

    Start with the most significant digit (A in my example), and convert that to a binary value (10). Multiple that value by 16, then add the binary value of the least significant digit (5).

    This gives you 10 * 16 + 5 = 165.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dre View Post
    ok i'll give it a shot. i think that with the user input i'd have to start by defining say

    char input1;
    char input2;

    and then i'd use scanf to store the user's chars into these char variables and work from there.

    in char two_characters[2] i don't see where the user input comes in, to me it just looks like its creating a character array with two slots.

    not sure though.
    As opposed to "char input1; char input2" which creates ... two slots of characters?

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    Quote Originally Posted by tabstop View Post
    As opposed to "char input1; char input2" which creates ... two slots of characters?
    ok im not going to pretend i know close to as much as you do but here's what i thought was going on:

    with input1; input2; method i thought i'd first store the user input and then somehow convert that char variable to an 8 bit binary which would then be stored into an array of 8 slots. so here's where my question to you guys came in because i wasn't sure how i would convert the character to its binary equivalent.

    with two_characters[2], as far as I know im creating an array with two slots, which say if the user inputs 'a' and '=' then two_characters[0] will hold 'a' and two_characters[1] will hold '='.

    but that is different than an array which holds 8 bits as far as I know. unless this array your suggesting has nothing to do with the binary array.

    Well, I'm assuming you want to convert two character nibbles to a binary byte. I think it's probably safe for you to assume that a byte can be represented by an unsigned char which will hold 8 bits.

    This means you just need to convert a string "A5", to a binary equivalent of 165 and store it in an unsigned char variable.

    Start with the most significant digit (A in my example), and convert that to a binary value (10). Multiple that value by 16, then add the binary value of the least significant digit (5).

    This gives you 10 * 16 + 5 = 165.
    thanks for the advice but it's a little off. i'm not trying to convert a string that consists of two characters into one binary big binary number. instead i give the user the ability to input to characters. and both characters will have their own array of their binary equivalent. so essentially i'd have two bytes. should've made it clearer, sorry.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dre View Post
    ok im not going to pretend i know close to as much as you do but here's what i thought was going on:

    with input1; input2; method i thought i'd first store the user input and then somehow convert that char variable to an 8 bit binary which would then be stored into an array of 8 slots. so here's where my question to you guys came in because i wasn't sure how i would convert the character to its binary equivalent.

    with two_characters[2], as far as I know im creating an array with two slots, which say if the user inputs 'a' and '=' then two_characters[0] will hold 'a' and two_characters[1] will hold '='.

    but that is different than an array which holds 8 bits as far as I know. unless this array your suggesting has nothing to do with the binary array.
    So basically you need to internalize this fact: everything the computer stores, is stored in binary. When you type '=', there are not small elves inside your computer carving an equal sign into a small piece of granite to be stored somewhere. If you think there is some distinction between a "char" and "eight bits of binary", you need to go back and read this paragraph again until you don't.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    how am I able to play around with the binary then ?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dre View Post
    how am I able to play around with the binary then ?
    The bitwise operators are &, |, ~, and ^.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    And the shift operators >> <<
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    20
    thank you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert char to binary???
    By chloe in forum C Programming
    Replies: 8
    Last Post: 09-24-2009, 12:31 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM