C ++字节,sprintf,十六进制。将字节转换为二进制

问题描述 投票:0回答:1

我正在这个具有这种格式/命令的项目中工作。我的问题是b0,b1..b5。我不太擅长C ++。我可以在C#中执行此操作。

Configure Image Data (CID) Command
Esc*v6W b0 b1 b2 b3 b4 b5
Where:
• 6 = the number of bytes following the command
• b0 = byte 0 = the color space
• b1 = byte 1 = the Pixel Encoding mode
• b2 = byte 2 = the number of bits per index or palette size
• b3 = byte 3 = the number of bits in the color component
• b4 = byte 4 = the number of bits in the color component
• b5 = byte 5 = the number of bits in the color component
Bytes 0 through 5 must contain binary data, not ASCII.

我应该如何在c ++中执行此操作。这是我到目前为止所拥有的。

int srcBitsPerPixel = 24;
    BYTE bitsPerIndex = 0x00;
    std::string str;
    std::vector<unsigned char> seq(6);
    char bufv6[6];
    StringCchPrintfA(bufv6, 6, "%c%s", 27, "*v6W"); // 5 chars
    MoveMemory(pOemPDEV->pBufStart + dwOffset, bufv6, 6);
    dwOffset += 5;

    seq[0] = (BYTE)0x02; // 0: ColourSpace
    seq[1] = (BYTE)0x03; // 1: Pixel Encoding Mode
    seq[2] = (BYTE)0x00; // 2: Bits Per Index
    seq[3] = (BYTE)0x08; // 3: Bits Per Component
    seq[4] = (BYTE)0x08; // 4: Bits Per Component
    seq[5] = (BYTE)0x08; // 5: Bits Per Component   

    for (int i = 0; i < 6; i++)
    {
        char bufV6W[4];

        StringCchPrintfA(bufV6W, 4, "%02X", seq[i]);
        str.append(bufV6W);
    }

    char v6[50];
    StringCchPrintfA(v6,50, "%c%s",27, str);
    MoveMemory(pOemPDEV->pBufStart + dwOffset, v6, 50);
    dwOffset += 1;

但是我没有得到正确的结果。谁能提供一些建议。这是c ++中的HP PCL。我同意,这种方式可能非常古老,而不是C ++标准。

这里是在HxD编辑器中的外观enter image description here

c++ hex printer-control-language
1个回答
1
投票

从那里使用的功能来看,您可能需要构建该序列并将其输出到流中(但是您下次应该真正尝试更好地解释它,因此请尝试这样做:

std::vector<unsigned char> v = { '*', 'v', '6', 'W',
                                0x02,   // b0: ColourSpace
                                0x00,   // b1
                                0x08,   // b2
                                0x08,   // b3
                                0x08,   // b4
                                0x08 }; // b5

std::copy(v.begin(), v.end(), pOemPDEV->pBufStart + dwOffset);
© www.soinside.com 2019 - 2024. All rights reserved.