C++将8位位集转换为无符号长,VS 2010-不工作

C++ convert 8 bit bitset to unsigned long, VS 2010 - not working

本文关键字:VS 2010- 工作 无符号 8位 转换 C++      更新时间:2023-10-16

下面的代码首先通过两次,然后第三次转换到ulong失败,给我一个0XCF而不是0xF3。知道问题出在哪里吗??似乎是VS 2010 to_long中的一个错误。二进制"11110011"应转换为十六进制F3!以下是在VS2010下运行调试时的结果。

第一时间bc_bit_char b'11000011'转换为k=x'000000c3';第二次:bc_bit_char b'00111100'转换为k=x'0000003c'第三次:bc_bit_char b'11110011'转换为k=x'000000cf'错误!s/b x 000000f3'

    std::bitset<8> bc_bit_char (00000000);
    unsigned char bc_char=' ', bc_convert_char=' ';
    unsigned long k=0, bc_rows=0;
    k = bc_bit_char.to_ulong(); // convert 8 bits to long integer with same bits 
    bc_convert_char = static_cast<unsigned char> (k); // convert long integer to unsigned 

对于哪个位最重要和哪个位最不重要存在混淆。在您提供的二进制常量中,最左边的位是最低有效位0。在to_long提供的十六进制值中,最左边的比特是最高有效的比特7。如果你颠倒其中一个,那么它们将是相等的。