# Thread: convert a char to its binary equivalent?

1. ## 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. Originally Posted by dre
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. 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. 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.

5. Originally Posted by dre
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. Originally Posted by tabstop
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. Originally Posted by dre
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. how am I able to play around with the binary then ?

9. Originally Posted by dre
how am I able to play around with the binary then ?
The bitwise operators are &, |, ~, and ^.

10. And the shift operators >> <<

11. thank you guys.