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 6354371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:33:53+00:00 2026-05-24T22:33:53+00:00

I have a need to serialize int, double, long, and float into a character

  • 0

I have a need to serialize int, double, long, and float
into a character buffer and this is the way I currently do it

int value = 42;
char* data = new char[64];
std::sprintf(data, "%d", value);

// check
printf( "%s\n", data );

First I am not sure if this is the best way to do it but my immediate problem is determining the size of the buffer.
The number 64 in this case is purely arbitrary.
How can I know the exact size of the passed numeric so I can allocate exact memory; not more not less than is required?

Either a C or C++ solution is fine.

EDIT
Based on Johns answer ( allocate large enough buffer ..) below, I am thinking of doing this

char *data = 0;
int value = 42;
char buffer[999];
std::sprintf(buffer, "%d", value);
data = new char[strlen(buffer)+1];
memcpy(data,buffer,strlen(buffer)+1);

printf( "%s\n", data );

Avoids waste at a cost of speed perhaps. And does not entirely solve the potential overflow
Or could I just use the max value sufficient to represent the type.

  • 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-24T22:33:56+00:00Added an answer on May 24, 2026 at 10:33 pm

    In C++ you can use a string stream and stop worrying about the size of the buffer:

    #include <sstream>
    
    ...
    
    std::ostringstream os;
    int value=42;
    os<<42; // you use string streams as regular streams (cout, etc.)
    std::string data = os.str(); // now data contains "42"
    

    (If you want you can get a const char * from an std::string via the c_str() method)


    In C, instead, you can use the snprintf to “fake” the write and get the size of the buffer to allocate; in facts, if you pass 0 as second argument of snprintf you can pass NULL as the target string and you get the characters that would have been written as the return value. So in C you can do:

    int value = 42;
    char * data;
    size_t bufSize=snprintf(NULL, 0 "%d", value)+1; /* +1 for the NUL terminator */
    data = malloc(bufSize);
    if(data==NULL)
    {
        // ... handle allocation failure ...
    }
    snprintf(data, bufSize, "%d", value);
    // ...
    free(data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have need to pack four signed bytes into 32-bit integral type. this is
This is the scenario: I have nested classes and need to serialize then in
I have a business class which I need to serialize to xml. It has
I have a simple Java class that I need to serialize to be stored
I have a DOM Document created from scratch and I need to serialize it
I have a class that I need to binary serialize. The class contains one
I have a class that I need to be able to serialize to a
I have an class defining an immutable value type that I now need to
I have an object which need to be serialized. Object to serialize: public class
I have an object graph that I need to serialize to xml and save

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.