I have a bunch of floats and I need to convert them to string to use Send command to send to a Receiver. However, I been trying to code like below where long1deg is a float. char long1degC = long1deg; Or

char* long1degC = new char[30];
 
    float long1deg= 2.4567F;
    sprintf(long1degC, "%.4g", long1deg);

but I always get errors every time. Any pointer will be really appreciated.

by the way, does this Send command is different from the send() in c++ library?

char buffer[1024];
	char tbuffer[64];
	size_t sz;

	if (initWinSockets())	return 1;

	s = new UdpSocket;
	if (s->OpenSend("127.0.0.1", 65500))
	{
		printf("UDP open error\n");
		while (!_kbhit()){}
		return 1;
	}
	
    /* Display UTC. */
    __time64_t ltime;
    __time64_t llast;
    struct tm gmt;
   _time64(&llast);
   _time64(&ltime);

   while (!_kbhit())
   {
	   _time64(&ltime);
	   if (ltime != llast)	//  Has the secconds counter changed yet?
	   {
			llast = ltime;
			_gmtime64_s(&gmt, &ltime);
			asctime_s(tbuffer, 64, &gmt);

			sz = sprintf_s(buffer, 1024, "Message sent from UDPsend at %s", tbuffer);
			printf("Send==>%s", buffer);
			s->Send(buffer, sz+1);
	   }
   }
	closeWinSockets();
	return 0;

I tried to type in like below but the command does not send any info. char buffer[1024] = {long1degC,long1minC,long1secC,lat1degC,lat1minC,lat1secC,long2degC,long2minC,long2secC,lat2degC,lat2minC,lat2secC};

Edited 5 Years Ago by yurigagarin: n/a

I have a bunch of floats and I need to convert them to string to use Send command to send to a Receiver. However, I been trying to code like below where long1deg is a float. char long1degC = long1deg; Or

char* long1degC = new char[30];
 
    float long1deg= 2.4567F;
    sprintf(long1degC, "%.4g", long1deg);

but I always get errors every time. Any pointer will be really appreciated.

unfortunately my psychic powers aren't working tonight so I am unable to divine what the errors are. Maybe you should tell us.

And you should be using CODE tags, not ICODE tags.

#include <sstream>
#include <iostream>

int main()
{
  float i = 5.2314;
  std::string s;
  std::stringstream out;
  out << i;
  s = out.str();
  std::cout << s;
  return 0;
}
This question has already been answered. Start a new discussion instead.