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...

10 Years
Discussion Span
Last Post by Nick Evan

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.

Edited by Nick Evan: n/a


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.

Edited by Nick Evan: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.