Newbie question...I fail =\

Ok so last night I was engaging a friend in a war of talking in different languages(morse code, binary, hex, english...etc) all that good stuff...

Anyhow I wanted to start typing longer sentences and not have to translate the binary from the Ascii decimal value by hand(yes I know there are translators...that do this for you), but I thought. Hey I'm learning C++ why don't I try to write something to convert this for me, should be easy right?

Well I guess I forgot one little detail from the book I'm reading...or something :P

Because when I started testing it all I got was the return value of the memory address instead of the value I wanted :P.

Btw...I dunno if trying to use a 2d array to represent the 26 binary values I would need was a good idea or not...sounded good at the time.

Thanks for any help...

Si1v3r


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Newb trying to do Pseudo-code \/
//Get input line - cin.getline(), cin.get()
//Check each character
//Evaluate againt binary table
//Convert to binary
//Output to cout

#include <iostream>

using namespace std;

int main()
{
    const int btable[26][8] =
    {
        {0110, 0000}  //Example binary. For testing I tried splitting it up since I didn't know if it could handle the full 8bit value the way I was doing it.
    };
cout << btable[0] << endl;
return 0;
}
Last edited on
for starters.... btable is a 2D array, so btable[0] is a 1D array. btable[0][0] would be the first element in the array.

For another thing, numbers starting with a 0 are octal, not decimal. So doing this:

cout << 010; will actually print "8", not "10". You'll have this problem with your values.

And lastly, a giant lookup table is a bad idea.



You could just pull out and print the individual bits using bitshifting and the AND operation.

Hint:

1
2
3
4
5
6
7
8
9
10
char v = 'A';

if(v & 0x80)
{
  // this will be run if the high bit of 'v' is set
}
else
{
  // this will be run if the high bit of 'v' is clear
}
Awesome :D, Alright I'm going to rewrite this...I won't be defeated by such a simple program :P.

Thanks for the help.
Topic archived. No new replies allowed.