C++ 如何将浮点数转换为无符号字符?

4

我是C++的新手,通过一些搜索我认为sprintf可以胜任,但编译时出现错误,提示我无法在unsigned charchar之间进行转换。我需要一个unsigned char,因为我将要打印到图像文件中(0-255 RGB)。

unsigned char*** pixels = new unsigned char**[SIZE];
vector<float> pixelColors;

...

sprintf(pixels[i][j][k], "%.4g", pixelColors.at(k));

(pixelColors的大小为3,'k'是一个'for循环'变量)


你想将浮点数转换为字符串吗?还是想打印浮点值的二进制表示? - Martin York
我编辑了我的主要帖子。我有一个三维动态数组,其中包含无符号字符,并希望存储每个浮点数的字节值。 - Steve
等一下,你的意思是要将图像数据发送到打印机吗?如果是这样,那么你需要查找CUPS(通用Unix打印系统)或Windows的等效软件。 - Frank
正如其他人所说 - 浮点数的范围是多少?是0.0到1.0,还是其他什么?您需要将浮点数范围缩放到无符号字符范围。 - Jeff
@Frank 不,只是一个图像文件。 @Jeff 从0到255。 - Steve
好的,那就容易多了 ^-^ - Frank
2个回答

8
我猜这些浮点数的范围在0.0到1.0之间,那么你可以按照以下方式实现:
float redf = 0.5f;
unsigned char reduc = redf * 255;

变量reduc现在为128。


编辑:完整示例,以Net PPM格式输出图像。

// Usage 
//  program > file.ppm

#include <vector>
#include <iostream>

typedef struct
{   /* colors in range 0..1 anything else is out of gamut */
    float red, green, blue;
} color;

using namespace std;

int main ( int argc, char **argv )
{   
    int width = 10, height = 10;
    vector<color> bitmap; // This should maybe be called floatmap? ;)

    // Make an image in memory as a float vector

    for( int y = 0; y < height; y++ )
    {   
        for( int x = 0; x < width; x++ )
        {
            color temp;
            temp.red = ((float)x / width);
            temp.green = 0;
            temp.blue = ((float)y / height);
            bitmap.push_back(temp);
        }
    }

    // output image as an Netppm pixmap
    cout << "P3" << endl << width << " " << height << endl << 255 << endl;
    for( int y = 0; y < height; y++ )
    {   
        for( int x = 0; x < width; x++ )
        {
            int red, green, blue;
            red = (unsigned char)(bitmap[y*width+x].red * 255);
            green = (unsigned char)(bitmap[y*width+x].green * 255);
            blue = (unsigned char)(bitmap[y*width+x].blue * 255);
            cout << red << " ";
            cout << green << " ";
            cout << blue << " ";
        }
        cout << endl;
    }
    return 0;

我希望这能对您有所帮助。您可以在维基百科上了解Netpbm格式


编辑2: 图像输出是纯文本。
结果如下:
test image (很小,不是吗?将第16行编辑为512x512或其他大小)

实际输出如下:

P3
10 10
255
0 0 0 25 0 0 51 0 0 76 0 0 102 0 0 127 0 0 153 0 0 178 0 0 204 0 0 229 0 0 
0 0 25 25 0 25 51 0 25 76 0 25 102 0 25 127 0 25 153 0 25 178 0 25 204 0 25 229 0 25 
0 0 51 25 0 51 51 0 51 76 0 51 102 0 51 127 0 51 153 0 51 178 0 51 204 0 51 229 0 51 
0 0 76 25 0 76 51 0 76 76 0 76 102 0 76 127 0 76 153 0 76 178 0 76 204 0 76 229 0 76 
0 0 102 25 0 102 51 0 102 76 0 102 102 0 102 127 0 102 153 0 102 178 0 102 204 0 102 229 0 102 
0 0 127 25 0 127 51 0 127 76 0 127 102 0 127 127 0 127 153 0 127 178 0 127 204 0 127 229 0 127 
0 0 153 25 0 153 51 0 153 76 0 153 102 0 153 127 0 153 153 0 153 178 0 153 204 0 153 229 0 153 
0 0 178 25 0 178 51 0 178 76 0 178 102 0 178 127 0 178 153 0 178 178 0 178 204 0 178 229 0 178 
0 0 204 25 0 204 51 0 204 76 0 204 102 0 204 127 0 204 153 0 204 178 0 204 204 0 204 229 0 204 
0 0 229 25 0 229 51 0 229 76 0 229 102 0 229 127 0 229 153 0 229 178 0 229 204 0 229 229 0 229 

如果浮点数值的范围不同,那么在函数中需要改变什么? - Christos Karapapas

2

不确定您的确切需求是什么[因为您没有像Greg要求的那样粘贴代码片段],以下示例可能会解决它:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    float i=1;
    unsigned char c;
    c = static_cast<unsigned char>(i);
    cout << c << endl;
    getch();
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接