Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3221466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:52:45+00:00 2026-05-17T15:52:45+00:00

I’m new to C++, and doing a bit of googling I thought sprintf would

  • 0

I’m new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can’t convert between an unsigned char and a char. I need an unsigned char because I am going to print to an image file (0-255 RGB).

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

...

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

(pixelColors has size of 3 and ‘k’ refers to a ‘for loop’ variable)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:52:46+00:00Added an answer on May 17, 2026 at 3:52 pm

    I’ll guess that the floats are in the range 0.0 … 1.0, then you do it like this:

    float redf = 0.5f;
    unsigned char reduc = redf * 255;
    

    The variable reduc is now 128.


    EDIT: complete example, outputting image in Net PPM format.

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

    }

    I hope this helps you. You can read about Netpbm format on wikipedia.


    EDIT2: The image output is clear text.
    Result is like this:
    test image (Tiny, isn’t it? edit line 16 to 512×512 or something)

    And the actual output is this:

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.