convert int to char

Hi there! I am planning to convert an integer value into my char array. Below is the related code:

int16_t packetnum = 0;
void loop()
char radiopacket[25];
packetnum++;
char chr_num[packetnum];
sprintf(radiopacket,%s,chr_num);

It supposed to show the increment every time.
But it show infinity zero at serial monitor..

Anyone could help me on this??

char chr_num[packetnum];

This will create an array but you never put anything in it.

Assuming you want to see packetnum

sprintf(radiopacket,"%d",packetnum);

Not tested

sterretje:

char chr_num[packetnum];

This will create an array but you never put anything in it.

Assuming you want to see packetnum

sprintf(radiopacket,"%d",packetnum);

Not tested

Thanks for pointing out the %d! It shows digit now but is not from zero and it is decreasing instead..
Even if I changed to :

++ packetnum;

still showing the same issue

char chr_num[packetnum];

In your code snippet this declares an array with zero elements, ie the value of packetnum. It is just as well that you don't try to put anything in it

It shows digit now

Please post the complete sketch that has this problem

UKHeliBob:

char chr_num[packetnum];

In your code snippet this declares an array with zero elements, ie the value of packetnum. It is just as well that you don't try to put anything in it

Do you mind to explain more on the problem? I don't really get the meaning..

UKHeliBob:
Please post the complete sketch that has this problem

Sorry but it is kind of difficult for me to upload the completed one. That's all the related part in the coding

When you declare an array and don't define the values at the same time you must tell the compiler how many elements the array is required to hold so that it can allocate the required memory based on the number of elements and their type

So, something like this

char anArray[8];  //an array with room for 8 chars

int anArray[10]; //an array with room for 10 ints[/code]

Your code does this

char chr_num[0];  //an array with room for zero chars

Sorry but it is kind of difficult for me to upload the completed one.

The write a small but complete sketch that illustrates the problem and upload that

UKHeliBob:
When you declare an array and don't define the values at the same time you must tell the compiler how many elements the array is required to hold so that it can allocate the required memory based on the number of elements and their type

So, something like this

char anArray[8];  //an array with room for 8 chars

int anArray[10]; //an array with room for 10 ints[/code]

Your code does this

char chr_num[0];  //an array with room for zero chars

but in my case, the array values in unknown, it depends how many message being sent

So declare the array with the largest number of elements that you will ever receive

Try using my SafeString library which will let you print() to your packet and give you an error message if you try to add too much data

Try

#include "SafeString.h"
// download SafeString library from Arduino library manager
// or from the tutorial page
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);  //
  }
  Serial.println();
  SafeString::setOutput(Serial); // enable error msgs
}

createSafeString(sfRadioPacket, 25); // space for 25 chars

int packetNum = 5676;
void loop() {

  sfRadioPacket = ""; // clear it when needed
  sfRadioPacket.print(packetNum); // you can print to a SafeString
  // print your other data here
  // SafeString will tell you if you overflow the packet
  Serial.println(sfRadioPacket.c_str()); // print the underlying char[]
  // send it to your radio library
  // radio.send(sfRadioPacket.c_str(),sfRadionPacket.length());
  while (1) {} // stop here
}

UKHeliBob:

char anArray[8];  //an array with room for 8 chars

You mean 7 characters, don't you?

You mean 7 characters, don't you?

Well it depends how you are going to use it.
There is a mixture here of char[] (for the packet) and '\0' terminated string arrays for sprintf
Easy to confuse the two.

That off-by-one error is one of the errors SafeString avoids (guards against)

createSafeString(sfRadioPacket, 25); // space for 25 chars

underneath creates a char[26] to allow for the terminating '\0' for the string

Then when you get to the
radio.send( ) the sfRadioPacket.length() only counts the chars not the terminating string '\0'

Does this help?

int16_t packetnum = 0;


void setup()
{
  Serial.begin(115200);
}


void loop()
{
  char radiopacket[25];
  packetnum++;
  // char chr_num[packetnum];
  sprintf(radiopacket, "%d", packetnum);
  Serial.println(radiopacket);
  delay(1000);
}

It's not clear why you don't use:

void loop()
{
  packetnum++;
  Serial.println(packetnum);
  delay(1000);
}

SteveMann:
You mean 7 characters, don't you?

No, I mean what I said. Room for 8 chars, one of which is the terminating '\0'

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.