473,513 Members | 3,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float to char*

Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);
Jul 22 '05 #1
4 11797

"Luca" <lu***@katamail.com> wrote in message
news:p%*********************@news4.tin.it...
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);


Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason it
really better to do things that safer way and use std::string and
std::ostringstream.

#include <string>
#include <sstream>

std::ostreamstream str;
str << "Object Pos: " << floatValue;
DrawText(x, y, str.str().c_str());

john
Jul 22 '05 #2
Thanks a lot.

Luca.
"John Harrison" <jo*************@hotmail.com> ha scritto nel messaggio
news:2u*************@uni-berlin.de...

"Luca" <lu***@katamail.com> wrote in message
news:p%*********************@news4.tin.it...
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);


Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::ostringstream.

#include <string>
#include <sstream>

std::ostreamstream str;
str << "Object Pos: " << floatValue;
DrawText(x, y, str.str().c_str());

john

Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"Luca" <lu***@katamail.com> wrote in message
news:p%*********************@news4.tin.it...
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);

Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::ostringstream.

#include <string>
#include <sstream>

std::ostreamstream str;


you mean...

std::ostringstream str;

? (or was it ostrstream, I forget)
str << "Object Pos: " << floatValue;
DrawText(x, y, str.str().c_str());

john

Jul 22 '05 #4

"Howard" <al*****@hotmail.com> wrote in message
news:O1******************@bgtnsc05-news.ops.worldnet.att.net...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2u*************@uni-berlin.de...

"Luca" <lu***@katamail.com> wrote in message
news:p%*********************@news4.tin.it...
Hi all, there's a way to convert a float to a char*? I have to do this:

char* str = "Object Pos: ";
char* str1 = //convert my float value to char*;
char* s = strcat(str, str1);
DrawText(x, y, s);


Simple way is to use a char array (not char*) and sprintf

char str[222];
sprintf(str, "Object Pos: %g", floatValue);
DrawText(x, y, str);

Of course it is VITAL that your array is big enough. And for this reason
it
really better to do things that safer way and use std::string and
std::ostringstream.

#include <string>
#include <sstream>

std::ostreamstream str;


you mean...

std::ostringstream str;


Yes, thanks for the correction.

john
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
32274
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
20
3119
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51 cm ---1 inch = 2.54/100 Meter ---1 yard = 3 feet ---1 feet = 12 inch
6
7595
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
9
30477
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
9
3906
by: hurcan solter | last post by:
Hi all, I am trying to convert a float value to a octet stream for transmission, I came up with solution like float deneme=3.14156789; float deneme2=0.0; vector<unsigned charvec; //this is my buffer can con contain other things besides this float int* val=reinterpret_cast<int*>(&deneme); // gives me jeebies vec.push_back((unsigned char)...
2
2208
by: Rupali12345 | last post by:
hi all, In following code itemdescription value is stored properly in file but the float values are stored as different symbols such as @,@A etc. If i want to store float values also as it is...what should I do?? #include <iostream> #include <conio.h> #include <fstream>
3
2698
by: docmur | last post by:
Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered EX: ./calc 200 + 200 or ./calc 200.20 + 200 Okay thats easy where I can't seem to get, my prof wants the input checked to see if the char *argv or char *argv contains a float or int. so I have to check for a...
14
5511
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
7
4452
by: DirtyRasa | last post by:
Here is what my professor told me to to. Write a function that converts a string to a float and returns a float. Test your function by entering f4 and with 4f. Both should say Data entered was not a number. Try again. and ask the user to type in another number. Here's what I have so far. Code:
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.