# Thread: User inputs two characters and...

1. ## User inputs two characters and...

I would like for my program to be able to give the binary values of these characters. I would then like to be able to work with these binary equivalents and be able to find the hamming distance between the two binaries, one's complement of them etc.

I was thinking maybe if I can somehow figure out their binary equivalents, I can create two arrays which hold these binaries and then will be able to compare the arrays to find the hamming distance and manipulate the binaries in the array to print out the one's complement and such.

Can anybody give me some hints on how to get started?

Thanks

2. Define "binary equivalent". Surely you don't mean
Code:
`char x = 'a'; //the binary equivalent of the character a`

3. Basically it's 8 bit ASCII value equivalent in binary...

'a' would be 01100001

4. Originally Posted by dre
Basically it's 8 bit ASCII value equivalent in binary...

'a' would be 01100001
So since it's essentially impossible for you to store the character in any other way, trying to convert into this form is futile.

5. Data stored in a variable (characters or otherwise) is already in a binary format. You can work with it as is. What makes 'a' show up as 'a' in a display on your screen is only a matter of representation of something that's already in binary. Conveniently enough, this wikipedia page has C code for computing hamming distance that can easily be converted to working with chars instead of ints.

6. Originally Posted by hk_mp5kpdw
Data stored in a variable (characters or otherwise) is already in a binary format. You can work with it as is. What makes 'a' show up as 'a' in a display on your screen is only a matter of representation of something that's already in binary.
Thanks. I read that yesterday and came up with this little code. I'm trying to get it to read the binary of the char and then store either an int 1 or 0 into an array i'm calling hamming3 and hamming4.

Code:
```printf("\n\nEnter two characters: ");
scanf("%c %c",&char1, &char2);

for(i=0; i < 2; i++) {
printf("The bit pattern for byte %d is: ", (i+ 1));
for(j=0; j < 8; j++) {

if (((*char1 & 1) != 1) && (i == 0)) {

printf("0");
++bitCount0;
hamming3[k] = 0;
k++;

}

if (((*char1 & 1) != 0) && (i == 0)) {
printf("%d", 1);
++bitCount1;
hamming3[k] = 1;
k++;

}

if (((*char1 & 1) != 0) && (i == 1)) {
printf("%d", 1);
++bitCount1;
hamming4[l] = 1;
l++;
}

if (((*char1 & 1) == 0) && (i == 1)) {
(++bitCount0);
printf("%d", 0);
hamming4[l] = 0;
l++;

}

*char1 >>= 1;
}

printf("\n");
++char1;
}```
However the program crashes after I type in two characters all it says prints is "The bit pattern is for byte 1 is: and then crashes.

It gives me the error:

Unhandled exception at 0x004126f8 in testing0.exe: 0xC0000005: Access violation reading location 0xcccccc61.

does anybody know why this is happening?

7. If char1 is a character, then *char1 is oh so wrong.

Alternatively, if char1 is a pointer-to-character, then &char1 is oh so wrong.