简体   繁体   中英

How to convert char(or string, other type) -> bits?

In c++, I don't understand about this experience. I need your help. in this topic, answers saying use to_string. but they say 'to_string' is converting bitset to string and cpp reference do too. So, I wonder the way converting something data(char or string (maybe ASCII, can convert unicode?). {It means the statement can be divided bit and can be processed it}

The question "How to convert char to bits?" then answers say "use to_string in bitset" and I want to get each bit of my input. Can I cleave and analyze bits of many types and process them? If I can this, how to?

#include <iostream>
#include <bitset>
#include <string>

using namespace std;

int main() {
    char letter;
    cout << "letter: " << endl;
    cin >> letter;
    cout << bitset<8>(letter).to_string() << endl;
    bitset<8> letterbit(letter);
    int lettertest[8];
    for (int i = 0; i < 8; ++i) {
        lettertest[i] = letterbit.test(i);
    }
    cout << "letter bit: ";
    for (int i = 0; i < 8; ++i) {
        cout << lettertest[i];
    }
    cout << endl;
    int test = letterbit.test(0);

}

When executing this code, I get result I want. But I don't understand 'to_string'. An important point is using of "to_string" {to_string is function converting bitset to string(including in name), then Is there function converting string to bitset??? Actually, in my code, use the function with a letter -> convert string to bitset(at fitst, it is result I want)}

help me understand this action.

Q: What is a bitset?

https://www.cplusplus.com/reference/bitset/bitset/

A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).

The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char).

In other words, a "bitset" is a binary object (like an "int", a "char", a "double", etc.).

Q: What is bitset<>.to_string()?

Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and members to_ulong and to_string). They can also be directly inserted and extracted from streams in binary format (see applicable operators).

In other words, to_string() allows you to convert the binary bitset to text.

Q: How to to I convert convert char(or string, other type) -> bits?

A: Per the above, simply use bitset<>.to_ulong()

Here is an example:

https://en.cppreference.com/w/cpp/utility/bitset/to_string

Code:

 #include <iostream> #include <bitset> int main() { std::bitset<8> b(42); std::cout << b.to_string() << '\n' << b.to_string('*') << '\n' << b.to_string('O', 'X') << '\n'; }

Output:

 00101010 **1*1*1* OOXOXOXO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM