我有以下算法用于将 24 位位图转换为像素的十六进制字符串表示形式:
// *data = previously returned data from a call to GetDIBits
// width = width of bmp
// height = height of bmp
void BitmapToString(BYTE *data, int width, int height)
{
int total = 4*width*height;
int i;
CHAR buf[3];
DWORD dwWritten = 0;
HANDLE hFile = CreateFile(TEXT("out.txt"), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
for(i = 0; i < total; i++)
{
SecureZeroMemory(buf, 3);
wsprintfA(buf, "%.2X", data[i]);
// only write the 2 characters and not the null terminator:
WriteFile(hFile, buf, 2, &dwWritten, NULL);
}
WriteFile(hFile, "\0", 2, &dwWritten, NULL);
CloseHandle(hFile);
}
问题是,我希望它忽略每行末尾的填充。例如,对于一个 2x2 位图,其中所有像素的值为 #7f7f7f,out.txt 的内容也包含填充字节:
7F7F7F7F7F7F00007F7F7F7F7F7F0000
我将如何调整循环以避免包含填充零?