# Characters to binary... hexadecimal to binary

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 10-22-2002
Trauts
Characters to binary... binary to characters
Basically what the title says... I need to be able to convert a string to binary... then convert that to hexadecimal (cout << hex << stringname; works fine for that part, but I can't save the output), and then the ability to change hexadecimal to binary and convert the binary to the original string... basically code and decode it...

I need the part converted from the characters (it has to have letter support) to binary to be a string so it uses hexadecimal for 0 and 1, not for the letters... so the integer data type wouldn't work.

I'm using Microsoft Visual C++ 6.0 as a compiler, and this is for a console application (DOS).

ANY help would be much appreciated!

Thanks!
• 10-23-2002
ammar
Do you mean a string that containes "10001011101" and to convert it into binary?

Maybe you can tokenize the string then use atoi().
• 10-23-2002
Trauts
Lets say I input the string "programming" into the program. It takes it as a string and puts the value "111 0000" for 'p' and "111 0010" for 'r' and so on.

So I'll end up with a long binary number representing the word "programming"

I don't want to use an insanely long switch statement, so a function would be nice.

I need to convert that to hexadecimal, and store that, too.

Then I need another routine that will convert the hexadecimal back to the binary, and convert the binary to text.

I don't think it is as hard as it sounds, but I don't know how to get the binary values for characters easily.

I'd also need to be able to convert numbers to binary.

THANKS!
• 10-23-2002
Stoned_Coder
for gods sake. computers only understand binary. Your chars are stored as a binary representation anyway. say char is 65 thats 00100001 in 1 byte. You can access the individual bits with c/c++ bitwise operators. This has been dealt with many times before so do a board search.
• 10-23-2002
Trauts
I did do a search.

And chill out... I know everything is stored in binary, but C++ doesn't have a command that converts it to binary... I know it is STORED in binary, but I need it usable.

I need it to convert NUMBERS and LETTERS.

And how might I convert to HEXADECIMAL and from HEXADECIMAL to BINARY?
• 10-23-2002
Trauts
Also, where can I learn about XOR encryption? I found an old thread and I need to start from what I already have of this encryption program.
• 10-23-2002
Hunter2
Quote:

I know everything is stored in binary, but C++ doesn't have a command that converts it to binary
What are you trying to say? Is this what you are trying to do?

Code:

```char stuff[] = "abcd"; char* asdf = changetobinary(stuff); //convert "abcd" to binary and store the string version in asdf```
Quote:

I need it to convert NUMBERS and LETTERS
To what?

Quote:

And how might I convert to HEXADECIMAL and from HEXADECIMAL to BINARY?
If you're talking about converting an int to hexadecimal/binary, then you are a retard, and can shut up right about now. Otherwise, if you are trying to convert a string which represents a hexadecimal number to a string representing a binary number, you can do the research yourself... first, figure out how to convert from hex to bin (and vice versa) and then figure out how to do it digit by digit or something; or, there might be useful functions for you to use that can convert a string in a certain base into an int, and then convert the int into a string using a certain base.

P.S. don't use caps; they don't make people want to help you. Instead, they make people want to smack you over the head with a large trout.
• 10-23-2002
Trauts
I meant to convert from base 10 to hexadecimal, yes, I know how to do it manually, but I don't want to use a switch statement if it is avoidable.

if the string was "Hello" (this is simplified because I don't want people to know everything about my program, or its useless except as an exersize...) then (yes, this is basically taken from another tutorial, it doesn't have everything I need, though http://www.cpp-home.com/print_t.pl?102)...

H - 72
e - 101
l - 108
l - 108
o - 111

so overall:
72 101 108 108 111

72 - 01001000
101 - 01100101
108 - 01101100
108 - 01101100
111 - 01101111

so...

01001000 01100101 01101100 01101100 01101111

and...

0100100001100101011011000110110001101111

divide that every 6 bits...

010010 000110 010101 101100 011011 000110 1111

and converting it back to base 10:

18 6 21 44 27 6 15

just going to hexadecimal for output is simple... like I said before, cout << hex << varname;

but I need it stored.
• 10-23-2002
Hunter2
Quote:

divide every six bits
:) There's your first problem. Hexadecimal doesn't mean dividing every 6 bits. But anyways, to do what you're trying, this is what I would do:

1) convert each letter of the input into a binary number, and append each such binary number (I mean, its digits) to a std::string.

2) create a tempory char[7] (or 6? Not sure if you need the null 0...).

3) Copy the first six characters of the std::string to the char array.

4) Use atoi() or strtol() or something to convert it into an integer, and store the result in a vector of ints. (no, I don't want to start up the old atoi() vs strtol() war again :( I heard strtol() is better though, but you can pick whatever you choose.)

5) Repeat 3 and 4 until finished.

Of course, you'll have to make sure you don't do an array index out-of-bounds error or anything, when you get to the last batch of characters in the std::string, in case you have fewer than 6 chars to copy to the temporary array.. you can use std::string::size() to check the size of the string, and then use modulus to figure out how many chars you have left at the end of the string, and only copy that many to the temporary array.

Hope this helps!

P.S. This is all theoretical, and might not work - but it's the best I can do right now, since I'm supposed to be doing homework ;)
• 10-23-2002
Stoned_Coder
Quote:

I know everything is stored in binary, but C++ doesn't have a command that converts it to binary...
Do you listen man?
Quote:

You can access the individual bits with c/c++ bitwise operators.
• 10-23-2002
Hunter2
lol i dont think that's what he meant in his question... look at his latest post :)
• 10-23-2002
Stoned_Coder
Quote:

but I need it stored.
Ever heard of stringstreams? Very similar to ostreams except they write to memory instead of the screen.
• 10-23-2002
Trauts
Hunter2, thanks for looking over some of my obvious stupidities and helping me anyway... lol

I meant that that was one layer of encrypting, the way the tutorial had it. I've thought about it for a little while, and the hexadecimal part I had planned IS pointless pretty much.

So really all I need (at the moment, I know how to do the rest) is how to save the text/number string in binary (as a string, not an integer... I need the leading 0s)

I also need to know how to reverse it.

I also need to understand XOR a lot better so that I can use that, too. How would I implement XOR as a function, so that I can run all this stuff different times... so a function to binary and back, etc...

I hope I make sense this time
• 10-23-2002
Trauts
I need to be able to go from a string like "Hello" to the ASCII code, and from that to binary.

I don't know how to do that without using a very very long switch statement.

I also have 100% no clue at all how to use "bitwise" or what "stringstreams" are

Oh, and one more thing: the 6 bits thing was dividing it up so that it would change the output... simple to do manually... char[x], etc.
• 10-23-2002
Eibro
PHP Code:

``` void PrintBinary(const char *pszString) {     int length = std::strlen(pszString);     using std::cout;     for (int position = 0; position < length; position++)     {         for (int i = 0; i <= std::UCHAR_MAX; i++)         {             cout << pszString[position] & std::pow(2, i);         }         cout << std::endl;     } };  ```
Something like that? May need some tweaking, I didn't test it... and i'm extremely tired.

Oh, and the headers you'll need
<climits>
<iostream>
<string>
<cmath>
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last