简体   繁体   English

将位域转换为字符串C ++

[英]convert bitfield to string C++

I want to convert a bitfield to a string. 我想将位域转换为字符串。 Visual Studio 2008 gives an invalid null pointer exception. Visual Studio 2008提供了无效的空指针异常。

Maybe it has something to do with the size of the array. 也许与数组的大小有关。 it must be 8 but the output says it is 4, but why? 必须为8,但输出显示为4,但是为什么呢?

class Converter
{
public:
    string bitfieldToString (bool b_input[])
    {
        string c_conv;
        int i;
        for(i = 0; i < sizeof(b_input) ; i++)
        {
            if(b_input[i]=false){
                c_conv.append("0");
            }
            else if (b_input[i]=true){
                c_conv.append("1");
            }
            else c_conv = "Input is not a bitfield";break;
        }
        cout<<c_conv<<" "<< sizeof(b_input)<<endl;
        return (0);
    }
};
int main(void)
{
    Converter converter;
    bool b2[8] = {0,1,0,0,1,0,1,1};
    converter.bitfieldToString(b2);
    return (0);
}

Thank you! 谢谢! Now everything works as intended. 现在一切都按预期进行。 and sorry for that dump question. 对这个转储问题深表歉意。 I am new to C++. 我是C ++的新手。

There is a lot wrong in your code. 您的代码有很多错误。

First of all, the null pointer exception comes from return (0); 首先,空指针异常来自return (0); at the end of the bitfieldToString fuction. bitfieldToString函数的末尾。 You have defined it to return a string ; 您已定义它以返回string when you return 0 instead, C++ thinks 0 is a char* pointer and will try to convert it - a NULL pointer - into a string, which will crash. 当您return 0 ,C ++认为0char*指针,并将尝试将其( NULL指针)转换为字符串,这将导致崩溃。 You should probably be returning c_conv instead. 您可能应该返回c_conv

Second, sizeof(b_input) will always be the size of a bool pointer. 其次, sizeof(b_input)将始终是bool指针的大小。 On a 32-bit system it will be 4, on a 64-bit system 8. You cannot get the length of an array passed as argument with sizeof ; 在32位的系统将是如图4所示,在64位系统8上不能获得与通过参数传递的数组的长度sizeof ; you will need to add a length parameter to your function. 您将需要在函数中添加一个length参数。

Third, inside your for loop, you are assigning to b_input[i] instead of comparing the values. 第三,在for循环中,您将分配b_input[i]而不是比较这些值。 Use == , not = . 使用== ,而不是=

Fourth, in the last else branch, you are missing braces. 第四,在最后一个else分支中,您缺少花括号。 Essentially, the break will always break out of the loop after the very first iteration. 本质上, break将在第一次迭代后始终跳出循环。

The exception is because you return (0); 例外是因为您return (0); . That's interpreted as a null pointer, used to initialise a std::string with a constructor which requires a valid pointer to a C-style string - not a null pointer. 这被解释为空指针,用于通过构造函数初始化std::string ,该构造函数需要指向C样式字符串的有效指针-而不是空指针。

That should be return c_conv; 那应该是return c_conv;

The size mismatch is because b_input isn't an array. 大小不匹配是因为b_input不是数组。 As a function parameter, bool b_input[] is a pointer. 作为函数参数, bool b_input[]是一个指针。 You can't pass an array to a function by value; 您不能按值将数组传递给函数; and there's no way to determine the array size from the pointer alone. 而且没有办法仅通过指针来确定数组大小。 So sizeof(b_input) gives you the size of a pointer, not the array, and everything goes wrong. 所以sizeof(b_input)给你一个指针的大小,而不是数组的大小,一切都会出错。

There are a few options. 有一些选择。 You could pass the size as a second parameter; 您可以将size作为第二个参数传递; but that's error-prone. 但这容易出错。 You could infer the size as a template argument, by taking the array by reference: 您可以通过引用引用数组来推断大小为模板参数:

template <size_t size>
string bitfieldToString (bool (&b_input)[size])

You could use std::array or std::vector , which have handy size() member functions. 您可以使用std::arraystd::vector ,它们具有方便的size()成员函数。 (But be careful with vector<bool> , since it's a special case that doesn't always behave quite like a standard container.) Or you could use std::bitset , which has a handy to_string function that does exactly what you want. (但是要小心vector<bool> ,因为这是一种特殊情况,并不总是表现得像标准容器。)或者您可以使用std::bitset ,它具有方便的to_string函数,可以完全满足您的要求。

Finally, enable your compiler's warnings - it should tell you not to use = where you mean == . 最后,启用编译器的警告-它应该告诉您不要在您的意思是==地方使用= And there's not much point checking for the case of a boolean being neither true nor false. 而且,对于布尔值既不是真也不是假的情况,没有太多的要点检查。 You can reduce the whole loop body to 您可以将整个循环体减少到

c_conv.append(b_input[i] ? '1' : '0');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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