Search code examples
c++hexprinter-control-language

C++ Byte, sprintf, hex. Convert byte to binary


I am working on this project which has this format/command. My problem is with b0,b1..b5. I am not very good at c++. I can do this in 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.

How should I do this in c++. This is what I have so far.

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;

But I am not getting the correct results. Can anyone provide some suggestions. This is HP PCL in c++. This way of doing may be very old, not C++ standards, I agree.

Here is how it looks in a HxD editor enter image description here


Solution

  • Judging by the functions used there, you probably need to build and output that sequence to a stream (but you should really try to explain better next time), so try this:

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