Bool数组[8]到char对话 [英] Bool array[8] to char conversation

查看:86
本文介绍了Bool数组[8]到char对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想将长度为8的bool数组转换为char。但它不起作用。原因是什么?



更新。从字面上看,我想要一点std :: bitset :: to_ulong函数。



我尝试过:





void print(bool * b){

unsigned char * c =(unsigned char *)(b);

std :: cout<< * c;

}

So I want convert bool array with length 8 to char. But it doesn't work. What are the reasons?

Upd. Literally I want somewhat that std::bitset::to_ulong function does.

What I have tried:


void print(bool *b) {
unsigned char *c = (unsigned char*)(b);
std::cout << *c;
}

推荐答案

的大小bool 值取决于实现(编译器)。它可能是一个字节或更多。 Common是 int 的大小。您可以通过打印 sizeof(bool)来检查它。



只有两种类型具有相同的大小,才能为不同类型生成指针。但即便如此,你应该知道你做了什么(知道如何解释和使用的是铸造数据)。



你试图打印出一个字符(使用 * 解除引用运算符)。该字符的值在此再次依赖于实现,但在大多数情况下,它应该是零或一个都不是可打印字符。



如果要打印 bool 您可以使用I / O操纵器来定义要打印的内容(false / true或0/1):

The size of bool values depends on the implementation (the compiler). It might be a single byte or more. Common is the size of an int. You can check it by printing sizeof(bool).

Casting a pointer for different types will only work if both types have the same size. But even then you should know what you do (know how to interpret and use the casted data).

You try to print out a single character (use the * dereference operator). The value of that character depends here again on the implementation but in most cases it should be zero or one which are both not printable characters.

If you want to print bool values you can use I/O manipulators to define what to be printed (false / true or 0 / 1):
cout << std::boolalpha << booleanValue << '\n';
cout << std::noboolalpha << booleanValue << '\n';



如果你需要一个真正的转换,你必须创建一个输出数组,迭代布尔输入数组,并分配相应的值:


If you need a real conversion, you have to create an output array, iterate over the boolean input array, and assign corresponding values:

unsigned char ucOut[8];
char strOut[9];
for (int i = 0, i < 8; i++)
{
    ucOut[i] = b[i] ? 1 : 0;
    // or
    //ucOut[i] = static_cast<unsigned char>(b[i]);
    strOut[i] = b[i] ? '1' : '0';
}
strOut[8] = '\0';
std::cout << strOut;

请注意,您可以在循环内部使用强制转换,因为这将转换单个值而不是数组。但它只能用于二进制值,而不能用于打印字符。





另请注意我使用过a static_cast 。如果在代码中使用指针,编译器将抛出错误。避免使用C ++进行旧的C样式转换。还有一个C ++强制转换运算符,可以从您的示例中转换指针。但是当使用这样的(重新解释)它(希望)表明代码的作者知道他在那里做了什么。

[/ EDIT]

Note that you can use casting inside the loop because that will cast a single value and not an array. But it can be only used for binary values and not for characters to be printed.


Note also that I have used a static_cast. If you use that in your code for the pointers, the compiler will throw an error. Avoid the old C style casting with C++. There is also a C++ cast operator that can cast the pointers from your example. But when using such (reinterpret) it (hopefully) indicates that the writer of the code knows what he is doing there.
[/EDIT]


你避风港不会。尝试

You haven't to. Try
#include <iostream>
using namespace std;

void print(bool *ba, size_t size)
{
  for ( size_t n = 0; n < size; ++n)
  {
    cout << ba[n];
    cout << ( n < size-1 ? ", " : "\n");
  }
}


int main()
{
  bool ba[] = { true, false, false };
  print(ba, sizeof(ba)/sizeof(ba[0]));
}


这篇关于Bool数组[8]到char对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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