C++ 字节到位转换然后打印

use*_*753 1 c++ byte for-loop bit-shift bit

代码取自:C 语言中的字节到二进制 图片来源:BSchlinker

我修改了以下代码,使其一次占用超过 1 个字节。我修改了它,让它工作了一半,然后对我的循环感到非常困惑。:( 我花了最后一天半的时间试图弄清楚......但我的 C++ 技能并不是那么好(仍在学习!)

#include <iostream> 
using namespace std; 

char show_binary(unsigned char u, unsigned char *result,int len);

int main() 
{
    unsigned char p40[3] = {0x40, 0x00, 0x0a};
    unsigned char bits[8*(sizeof(p40))];
    int c;
    c=sizeof(p40);

    show_binary(*p40, bits, 3);
    cout << "\n\n";

    cout << "BIN = ";
    do{
        for (int i = 0; i < 8; i++)
            printf("%d",bits[i+(8*c)]);
        c++;
    }while(c < 3);

    cout << "\n";

    int a;
    cin >> a;
    return 0;
}

char show_binary(unsigned char u, unsigned char *result, int len) 
{ 
    unsigned char mask = 1;
    unsigned char bits[8*sizeof(result)];
    int a,b,c;
    a=0;
    b=0;
    c=len;

    do{
        for (int i = 0; i < 8; i++)
            bits[i+(8*a)] = (u[&a] & (mask << i)) != 0;
        a++;
        }while(a < len);

    //Need to reverse it?
    do{
        for (int i = 8; i != -1; i--)
            result[i+(8*c)] = bits[i+(8*c)];
        b++;
        c--;
    }while(b < len);

    return *result;
}
Run Code Online (Sandbox Code Playgroud)

我吐完之后:

    cout << "BIN = ";
    do{
        for (int i = 0; i < 8; i++)
            printf("%d",bits[i+(8*c)]);
        c++;
    }while(c < 3);
Run Code Online (Sandbox Code Playgroud)

我想取 bit[11] ~ bit[the end] 并每 8 位计算一个 BYTE。如果这是有道理的话。但首先该功能应该可以工作。关于如何完成此操作有任何专业提示吗?当然,把我的代码拆开。我喜欢学习。

Der*_*ger 5

伙计,这段代码中发生了很多事情,所以很难知道从哪里开始。我只想说,你有点太努力了。听起来您正在尝试 1)传递字节数组;2) 将这些字节转换为二进制的字符串表示形式;3)将该字符串表示形式转回一个值?

碰巧我最近在 C 中做了类似的事情,它应该仍然可以使用 C++ 编译器工作。

#include <stdio.h>
#include <string.h>

/* A macro to get a substring */
#define substr(dest, src, dest_size, startPos, strLen)  snprintf(dest, dest_size, "%.*s", strLen, src+startPos)

/* Pass in char* array of bytes, get binary representation as string in bitStr */
void str2bs(const char *bytes, size_t len, char *bitStr) {
    size_t i;
    char buffer[9] = "";
    for(i = 0; i < len; i++) {
        sprintf(buffer, 
            "%c%c%c%c%c%c%c%c", 
            (bytes[i] & 0x80) ? '1':'0', 
            (bytes[i] & 0x40) ? '1':'0', 
            (bytes[i] & 0x20) ? '1':'0', 
            (bytes[i] & 0x10) ? '1':'0', 
            (bytes[i] & 0x08) ? '1':'0', 
            (bytes[i] & 0x04) ? '1':'0', 
            (bytes[i] & 0x02) ? '1':'0', 
            (bytes[i] & 0x01) ? '1':'0');
        strncat(bitStr, buffer, 8);
        buffer[0] = '\0';
    }
}
Run Code Online (Sandbox Code Playgroud)

要将二进制字符串恢复为一个值,可以通过位移位来完成:

unsigned char bs2uc(char *bitStr) {
    unsigned char val = 0;
    int toShift = 0;

    int i;
    for(i = strlen(bitStr)-1; i >= 0; i--) {
        if(bitStr[i] == '1') {
            val = (1 << toShift) | val;
        }

        toShift++;
    }

    return val;
}
Run Code Online (Sandbox Code Playgroud)

一旦你有了一个二进制字符串,你就可以获取任意 8 位(或者更少,我猜)的子串,并将它们转换回字节。

char *bitStr; /* Let's pretend this is populated with a valid string */
char byte[9] = "";
substr(byte, bitStr, 9, 4, 8); 
/* This would create a substring of length 8 starting from index 4 of bitStr */
unsigned char b = bs2uc(byte);
Run Code Online (Sandbox Code Playgroud)

如果您想看一下,我实际上已经创建了一整套值 -> 二进制字符串 -> 值函数。GitHub - binstr: GitHub