can you please help me how to convert characters to binary...

for example:
you have to convert the word "hello world" to its binary form...

i dont know how to do...
can you please give me some advice...

Recommended Answers

All 3 Replies

Assuming that you want a 8-bit binarynumber and your input contains only printable-chars:

First: convert the string (assuming it is a string) to a c string (with string.c_str() )
Now you'll have an array of chars which can be converted very easily to ints. Now convert this number to binary (by using the mod-operator for example) and you're done.

Or you could use bitwise-operators, but if you are new to programming, this won't make very much sense to you.

Did some reading and I have a better solution:

#include <bitset>
#include <iostream>

int main(void)
    std::cout << std::bitset<CHAR_BIT>( 'a' ) << std::endl;

This will give you the binary number for the char 'a'. Now all you have to do is make a loop that loops through the string and calls this function for every char in the string.

Just cast it to an integer.

Just cast it to an integer.

And then? It doesn't change miraculously into a binary number.

Just use the code I posted and you're halfway done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.