Binary Conversion

I heard there was a function to convert character values into Binary values.

Since character values represent ASCII numbers, I assume its just a regular decimal/binary conversion function.

I can not find it online. Can anyone direct me?
I'm not sure what you want. Numbers are stored in binary and you can use bitwise ops to do things with them. What kind of function are you actually looking for?
AFAIK there is no std function for this, but converting base 10 to base 2 is trivial.
Do you want to print the binary representation of a char?

1
2
char c = 'A';
std::cout << std::bitset<sizeof(c) * CHAR_BIT>(c) << std::endl;
What us the type of the value returned by bitset<[what type here?]>(char&)?
Ok, this is the kind of emplementation I want to end up with:

1
2
3
4
5
6
7
8
9
10
11
12
string conv_tobin(const string&)
{
    string temps = "";
    /*convert each character into a binary value, hopefully, if they are numbers they
    will be treated like integers*/
    
    /*convert this undefined binary type, which I assume is an integer
    to a string.*/

    /*Append the binary representation to the string*/
    return temps;
}


converting the string into it's binary representation as a string.
I'm about to tear my god dam hair out:

1
2
3
4
5
6
7
8
9
10
string conv_str_tobin(const string& s)
{
    string temps = "";
    bitset<sizeof(s)> bin(string(s));
    for(unsigned int x = 0; x < int(bin.size()); x++)
    {
        temps += conv<bool, string>(bin[x]);
    }
    return temps;
}


It wont compile NO MATTER WHAT.

bin.to_string() wont work.
bin.size() wont work
putting bin into a string stream all at once, or even bit-by-bit wont work.


I reasearched, etc.. etc...
I'm so close to just losing it over this thing.

common.cpp: In function 'std::string conv_str_tobin(const string&)':
common.cpp:133:16: error: request for member 'to_string' in 'bin', which is of non-class type 'std::bitset<4u>(std::string) {aka std::bitset<4u>(std::basic_string<char>)}'
     return bin.to_string();
                ^
common.cpp:134:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Last edited on
Maybe this will help http://ideone.com/bNMq6o
Thanks Naraku.

It would appear there were a few header files missing in Peter's suggestion, which were not included...


(-,-')

Thanks Naraku for clarifying.
Last edited on
Now, if we had the char '1', and the integer 1, would their binary representations be different, or same?
would their binary representations be different, or same?

Different.

Of course when you have some working code, you can test this for yourself.
But very simply (using decimal representation, not binary)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

    using namespace std;

int main()
{
    int number;
    
    number = 0;
    cout << "   number  0  : " << number << endl;
    
    number = '0';
    cout << "character '0' : " << number << endl;
        
    return 0;
} 
   number  0  : 0
character '0' : 48
Topic archived. No new replies allowed.