How do I concatenate int variables?

Sep 19, 2017 at 9:36pm
I have something like:

int MCC, MND, LAC, CID;

void loop()

MCC = (towerInfo->mobileCountryCode);
MNC = (towerInfo->mobileNetworkCode);
LAC = (towerInfo->locationAreaCode);
CID = (towerInfo->cellId);

In which, the output is something like, MCC: 250, MNC: 580, LAC: 68900, CID: 2350456.

I want a variable (e.g. CELL) that looks something like 250-580-68900-2350456. How do I do this? I've been looking for ages and trying out a lot of stuff, but can't quite find a solution to what seems like a simple-to-solve problem.
Last edited on Sep 19, 2017 at 9:38pm
Sep 19, 2017 at 9:53pm
integers are just bits. That is all. Any formatting you will have to do yourself. To do these things you may have to drop the int into a string, or store 4 integers and print them together.
eg
int dashed[4] = {250,580,68900,2350456};
cout << dashed[0] <<'-' << dashed[1]<<'-' ... etc

Sep 19, 2017 at 10:32pm
I don't quite follow. I want to something like a string-variable that stores these values. I don't just want to have it printed.
Last edited on Sep 19, 2017 at 10:32pm
Sep 19, 2017 at 10:33pm
Last edited on Sep 19, 2017 at 10:33pm
Sep 19, 2017 at 10:49pm
Any way to do it without <iostream> and <stream>? Due to the file size of these libraries I rather not use them. My code is about 24kb, with the inclusion of these libraries it suddenly peaked to 105kb.
Last edited on Sep 19, 2017 at 10:50pm
Sep 19, 2017 at 10:54pm
std::to_string is defined in the header <string>.

There is no standard library header named stream. (Maybe you meant sstream.)

If you don't want to use the STL strings (e.g., for code size purposes), you may also find std::itoa in <cstdlib>. But this is non-standard.
https://www.mkssoftware.com/docs/man3/itoa.3.asp

Also, std::sprintf() (should be preferred over itoa):
http://en.cppreference.com/w/cpp/io/c/fprintf
Last edited on Sep 19, 2017 at 11:02pm
Sep 19, 2017 at 11:21pm
Any way you could give a simple example of how I should embed this in my code? To make it even easier:

int LAC, CID;
String CELL;

LAC = (towerInfo->locationAreaCode); // 68900
CID = (towerInfo->cellId; // 2350456

Output-wanted is something like CELL = 68900-2350456




I assume it's something like:

#include <stdlib.h>
int LAC, CID;
String CELL;

LAC = (towerInfo->locationAreaCode);
CID = (towerInfo->cellId;
itoa (LAC, LACstr, 10);
itoa (CID, CIDstr, 10);

After that, would I be able to concatenate, doing something like CELL = ''LACstr'' + ''-'' + ''CIDstr''?



Apologies for the dumb questions; I literally have no prior experience with any coding.
Last edited on Sep 19, 2017 at 11:27pm
Sep 20, 2017 at 12:24am
Streams are what you use in C++ to convert between strings and other things.

1
2
#include <sstream>
#include <string> 
1
2
3
4
5
6
7
8
9
std::string& make_cell_number( TowerInfo* towerInfo )
{
  std::ostringstream ss;
  ss << towerInfo->mobileCountryCode << "-"
     << towerInfo->mobileNetworkCode) << "-"
     << towerInfo->locationAreaCode) << "-"
     << towerInfo->cellId;
  return ss.str();
}

Hope this helps.
Sep 20, 2017 at 12:44am
To concatenate C-strings you need to use std::strcat. Obviously std::strings are preferred, so you don't have to do that.

Using only <cstdio>:
1
2
3
4
static constepxr int max_sz = 64;
char output[max_sz];
int const result = std::sprintf(output, max_sz, "CELL = %d-%d", LAC, CID);
if (result < 0 || result >= max_sz) { /* handle error */ }; 


Using only <string>:
 
std::string const result = "CELL = " + std::to_string(LAC) + "-" + std::to_string(CID);


I literally have no prior experience with any coding.

I'm surprised.
Topic archived. No new replies allowed.