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

发布于 2024-12-07 20:02:53 字数 1597 浏览 0 评论 0原文

代码取自: Bytes to Binary in C Credit: 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;
}

在我吐出之后:

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

我想采取 bit[11] ~ bit[最后] 并每 8 位计算一个字节。但首先该函数应该可以工作。当然,我喜欢学习如何分解我的代码。

Code Taken From: Bytes to Binary in C Credit: BSchlinker

The following code I modified to take more than 1 Byte at a time. I modified it, and got it half working and then got really confused on my loops. :( Ive spent the last day and a half trying to figure it out... but my C++ skills are not really that good (still learning!)

#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;
}

After I spit out:

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

Id like to take bit[11] ~ bit[the end] and compute a BYTE every 8 bits. If that makes sense. But first the function should work. Any pro tips on how this should be done? And of course, rip my code apart. I like to learn.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浸婚纱 2024-12-14 20:02:53

伙计,这段代码中发生了很多事情,所以很难知道从哪里开始。我只想说,你有点太努力了。听起来您正在尝试 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';
    }
}

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

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;
}

一旦你有了一个二进制字符串,你就可以获取任意 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);

我实际上已经创造了一整套价值 ->二进制字符串->值函数(如果您想查看它们)。 GitHub - binstr

Man, there is a lot going on in this code, so it's hard to know where to start. Suffice to say, you're trying a bit too hard. It sounds like you are trying to 1) pass in a byte array; 2) turn those bytes into a string representation of the binary; and 3) turn that string representation back into a value?

It just so happens I recently did something similar to this in C, which should still work using a C++ compiler.

#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';
    }
}

To get the string of binary back into a value it can by done with bit shifting:

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;
}

Once you had a binary string you could then take substrings of any arbitrary 8 bits (or less, I guess) and turn them back into bytes.

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);

I've actually created a whole suite of value -> binary string -> value functions if you'd like to take a look at them. GitHub - binstr

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文