1

I need to generate a "package" (string) that consists in 3 velocities: xvel, yvel and wvel. The most convenient way to do so, is to multiply them by 100 and the convert them to hex. The problem is that I need a constant length in each velocity (in order to easily decode them after). I designed the following code, but I have no success when trying to concatenate every vector in a single "buffer". Any suggestions?

string buffer [50];

int x = 1.23*100;
int y = 0*100;
int w = 7.89*100;

string xPadle = "";
string yPadle = "";
string wPadle = "";

if(x <= 15) string xPadle = "00";
else if(x <= 255) string xPadle = "0";
else string xPadle = "";

if(y <= 15) string yPadle = "00";
else if(y <= 255) string yPadle = "0";

if(w <= 15) string wPadle = "00";
else if(w <= 255) string wPadle = "0";


sprintf (buffer, "%s%x%s%x%s%x", xPadle, x, yPadle, y, wPadle, w);
//printf("%s", buffer);
return 0;
M Aubel
  • 33
  • 2
  • 5
  • 3
    stake a look at using a `std::stringstream`, `std::setw` to do the padding, and `std::setfill` to pad with zeros. Eg: std::stringstream test; test << std::setfill('0') << std::setw(10) << std::hex << 10; std::cout << test.str(); – user4581301 Nov 30 '17 at 22:11
  • 1
    If you use `sprintf()` you can use `%03d` to print 3 digits with leading zeroes. – Barmar Nov 30 '17 at 22:14

0 Answers0