可以将比特组< 8>到一个整数字符数组? [英] is it possible to convert bitset<8> to an array of characters of integers?

查看:184
本文介绍了可以将比特组< 8>到一个整数字符数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 bitset< 8> v8 ,其值类似于11001101,二进制,我们如何将它转换为一个字符数组或整数在c ++?

I have bitset<8> v8 and its value is something like "11001101", something in binary, how can we convert it to an array of characters or integers in c++?

推荐答案

要转换为char数组,可以使用 bitset :: to_string()函数获取字符串表示形式,然后从该字符串中复制单个字符:

To convert to an array of char, you could use the bitset::to_string() function to obtain the string representation and then copy individual characters from that string:

#include <iostream>
#include <algorithm>
#include <string>
#include <bitset>
int main()
{
        std::bitset<8> v8 = 0xcd;

        std::string v8_str = v8.to_string();
        std::cout << "string form: " << v8_str << '\n';

        char a[9] = {0}; 
        std::copy(v8_str.begin(), v8_str.end(), a);
        // or even strcpy(a, v8_str.c_str());
        std::cout << "array form: " << a << '\n';
}

这篇关于可以将比特组&lt; 8&gt;到一个整数字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆