Combining strings, integers, characters?

I am still trying to teach myself C++ and have a problem I cant quite figure out

Uint_8 apples = 12345678
Const char* = "oranges"

Const char* Fruit = apples + "-" + Oranges

Expected result ————Fruit = “apples-oranges”

If you want us to look at your code, you should actually post the code that you have tried first.
The code above won’t even build as it has multiple issues:

  1. C/C++ is case sensitive so Uint_8 and Const are wrong.
  2. there is no type Uint_8. The closest to that would be uint8_t.
  3. each C/C++ line of code has to be terminated with semicolon ;
  4. uint8_t can only hold an 8 bit value (hence the name) which ranges from 0 to 255
  5. C strings cannot be constructed via the plus operator
  6. even if all that worked you’d not get the variable name in your string but only its value.
uint_8t* apples = 12345678;
const char* oranges = “abcdef”;
const char* fruits = apples + “-“ + oranges;

sprintf(fruits);

using uint8_t is working, but what should it be? end result will be alphanumeric, as i plan to convert this field to hex.
i want the printout to say 12345678-abcdef

Again, this is not code that would build.

Also a pointer to a uint8_t would not hold your value you assign but would rather point to the location you assign and accessing that pointer would give you back the byte at that memory location (providing it was a valid location).

I guess you rather want something like this

void someFn() {
  uint8_t apples = 255; // that's the MAXIUM value for unsigned 8bit inteters
  const char* oranges = "abcdef";
  char fruits[32];      // reserve 32 bytes for the resulting string to be stored in

  snprintf(fruits, sizeof(fruits), "%u-%s", apples, oranges);
  Serial.println(fruits);
}

You will have a lot less frustration by doing a basic fundamentals course - here is a free one that will set you up with some knowledge of the basics - https://www.w3schools.com/cpp/default.asp

3 Likes

you keep saying it won’t compile, but this is WORKING in my other projects.

byte mac [6];
uint8_t* ID = WiFi.macAddress(mac);

with that, my printout of ID is 111222333

I’m 100% certain that you haven’t got this in a project that actually builds.

uint_8t is not known but it should be uint8_t.

image

How do you do the printout?
And why would you want to catch the MAC address twice (in ID and mac)?
With ID = WiFi.macAddress(mac); you are just catching the same pointer that you already have with mac :wink:

This is the implementation of WiFi.macAddress()

    uint8_t* macAddress(uint8_t *mac) {
        memcpy(mac, wifi_config()->nw.uaMacAddr, 6);
        return mac;
    }

You pass in a pointer to a buffer where you want the data stored and then the function returns exactly that pointer you provided it with in the first place.

i was using mac in a test project, but that code is straight from the docs here. i think your print statement will work for me though. does the same still hold true if both values are strings?

If both variables were strings you’d need to reflect that in the format string (%u stands for unsigned and %s for string)

Have a look here for more info
http://www.cplusplus.com/reference/cstdio/printf/

on a roll now :wink: thanks mate!

ok, i’ve done confused myself good now

String Project = "RGB-";
 String DevID = System.deviceID();
 String my_ID = Project + DevID;
  char payload[128];
   sprintf(payload,"%s", my_ID);

and it’s printing x=
not rgb-1234567890123456789

When we were talking about strings we meant C strings not String objects.
If you want to use a String object (which I always advise against) in sprintf() then you need to convert that into a C string

  sprintf(payload,"%s", (const char*)my_ID);

This would be the warning the compiler throws for your version

i didn’t get any warnings in the IDE, it compiled and spat out the wrong code. the conversion worked though. is there a better way for my last code? why do you advise against Strings?

Yup, that is an unfortunate behaviour I’ve lamented frequently before.
You will only see the warnings when your code actually contains a real error.
Hence when my code builds but doesn’t behave as expected I usually add a stray curly brace at the end of my code to provoke an error and then see what warnings I might have produced.
(actually I do that in any case since I don’t like my code throwing warnings behind the scene and want to get rid of them for my own pride :blush:)

Because they use dynamic memory allocation which - over time - may lead to heap fragmentation which will cause issues which would be hard to debug (e.g. sudden crashes at unpredictable places, failing to reconnect after a connection loss, …).

With proper computers with loads of RAM and elaborate memory management String (or std::string) is safe to use, but on small embedded systems I’d avoid using it.

I’d rewrite your code this way

  const char* Project = "RGB-";
  char DevID[32];
  char my_ID[36];
  char payload[128];

  strcpy(DevID, (const char*)System.deviceID());
  snprintf(my_ID, sizeof(my_ID), "%s%s", Project, DevID);

But if I were only interested in the final result and not need the intermediate variables I’d go with something like this

  char id[64];
  snprintf(id, sizeof(id), "RGB-%s", (const char*)System.deviceID());
2 Likes

wow, thta just looks cleaner too. uploaded and working into my payload now. thank you for the help