0

I have been working on a project where I need to convert an int8_t variable to a string. I did a bit of reading on this and came to a conclusion that using Strint(int8_t) is an option.

Here is my code:

int8_t matt = 0;
matt += 1;
char string[3]=String(matt);
tft.textWrite(string);// this is used to display text on an lcd display (arduino)

For some reason the method I used did not work so I researched a bit more and found out String(matt) is actually a String object. I don't really know if this is true for a fact. In order for the tft.textWrite(); to work it should look something like this.

Here is the code:

 char string[15]= "Hello, World! ";
 tft.textWrite(string);

I tried using this also:

char string[3];
sprintf(string, "%ld", matt);

However this did not work.

Can anybody help?

Thanks

  • 2
    What's wrong with std::to_string(0)? – George Sep 06 '16 at 16:38
  • 1
    I can't use that in arduino –  Sep 06 '16 at 16:40
  • `char string[3]` should be `char string[5]` as it needs to hold e.g. `-100`, i.e. 4 chars and a termination. Also: What do mean by `However this did not work` ? In which way did it fail? – Support Ukraine Sep 06 '16 at 16:42
  • You can just convert back to a char array if you want to use the textWrite function, something like char string[1024]; std::strcpy(string, std::to_string(0).c_str()); – George Sep 06 '16 at 16:43
  • Because what happened is it starting printing weird characters for some reason on the screen. If I just say char string[3]="1" everything prints fine. –  Sep 06 '16 at 16:44
  • The "duplicate" answer does not tag arduino, so you should check the avr-libc itoa(int val, char* target, int radix) solution there. Less overhead than snprintf or String(int val). – datafiddler Sep 06 '16 at 20:41

4 Answers4

0

It should work fine.

sprintf(string, "%ld", matt);

%ld is used for long int. you are using int8_t which is of range -128 to +128, it is better to use %d.

0

This code is nearly right

char string[3];
sprintf(string, "%ld", matt);

first

"%ld" 

expects a long int as a type you need to find one that can print an 8 bit int. I think its "%hh".

second your

char string[3];

is too short! what if matt is 100 then you use 3 chars but you still need room for the ending zero (and sign). So it should be at least

char string[5];

third the code

sprintf(string, "%ld", matt);

is unsafe as it can write beyond the end of string as it would in your original code, use

snprintf(string, sizeof(string), "%ld", matt);
Surt
  • 15,501
  • 3
  • 23
  • 39
0

First you have to keep in mind that there is no String type in C, only fixed sized char arrays (type: char* or char[]). We still call them strings, but keep that in mind.

Your attempt with sprintf looked good to me. Are you shure your environment includes the full C Standard Library ? If not, you will have to code your own format function. If yes, have you included string.h ?

Anyway, try to use snprintf() to precise the size of the target, it is safer.

EDIT: This post applies to the good ol' C style, which I did not see was off topic

-1

the following code should work:

// Example program
#include <iostream>
#include <string>

int main()
{
  int8_t matt = 0;
  matt += 1;
  char string[1]; 
  sprintf(string, "%ld", matt);
  std::cout << string << std::endl;
  return 0;
}

it prints "1".