Author Topic: PAYLOAD convert a int to const char*  (Read 374 times)

Navidulce

  • RFduino Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
PAYLOAD convert a int to const char*
« on: April 30, 2017, 12:51:59 AM »
Hi everyone

I am a new user.


I saw a exemple in SimbleeCOM/ SEND. It have a  const char* payload. Could you help me, to convert a integer(int) to const char*?Please



Thank you

flurin4

  • RFduino Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: PAYLOAD convert a int to const char*
« Reply #1 on: May 04, 2017, 07:39:03 AM »
Hi Navidulce,

it is hard to help you in this issue with so little information at hand.
Could you specify what kind of functions you are using, what you are trying to do, or best if you give us some example code or a MWE (Minimal Working Example, of the problem)?

Regards,
FLurin

Navidulce

  • RFduino Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PAYLOAD convert a int to const char*
« Reply #2 on: May 05, 2017, 12:47:16 PM »

flurin, thank you for write it.

Now I have the solution. It is  "itoa" function.

Navidulce

  • RFduino Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PAYLOAD convert a int to const char*
« Reply #3 on: May 06, 2017, 11:22:27 AM »
Well I can't test it, now. Because the programing pins took out of the board. I welded the programing pins, again to the board. But I am worry if  it can creat a curtcircuit and break the board. Is there any ground plane around? or something dangerous.
 
When I test it:

int len= 8; //Exemple

serial.print( ' itoa (AnyNumer, payload, len));

Is not important, what value have AnyNumber the result is  1848391721. Why always  the same number? and why 1848391721?
« Last Edit: May 06, 2017, 11:24:35 AM by Navidulce »

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: PAYLOAD convert a int to const char*
« Reply #4 on: May 06, 2017, 01:20:57 PM »
You have to show us your sketch so we can run and see what you are actually doing.

Without that, like Flurin says, there is no way we can second guess what of a dozen errors you could be making in your code.

The variables you naming for itof are not what itof is doing. itof converts a hex value (your AnyValue variable) to a string (your Payload variable).
What you call payload is not a payload. It should be a pre-defined buffer for the converted string to go into.
What you call len is not a length. It is a definition of what BASE you want your string to represent the value as.
2=binary. 8=octal, 10=decimal, 16=hex

itof is not a standard C or C++ function. You have include <itof.h> for it to be recognized in Arduino/Simblee/RFduino.

Code: [Select]

#include <itoa.h>

void setup() {

  Serial.begin(9600);

char payload[100]; // define maximum size of string buffer.

int BASE = 8;   // 2=binary, 8=octal, 10=decimal, 16=hex

int AnyNumber = 122;

  Serial.println(itoa (AnyNumber, payload, BASE));
  Serial.println(payload);

}

void loop() {

}


Navidulce

  • RFduino Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: PAYLOAD convert a int to const char*
« Reply #5 on: May 07, 2017, 03:07:36 PM »
When I tested. I did it as following. I thought it worked, but now after test what I say in the last message, I am confused about if I works well.

I have to know if it works soon.

Code: [Select]
#include <itoa.h>
#include <SimbleeCOM.h>

const char* payload;
int BPM=0;

void setup{
SimbleeCOM.begin();
}

void loop{
BPM = analogRead(3);
SimbleeCOM.send(itoa(BPM,payload,16),16);
}

I appreciate your coments

Thank you
« Last Edit: May 07, 2017, 03:43:01 PM by tolson »

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: PAYLOAD convert a int to const char*
« Reply #6 on: May 07, 2017, 03:42:28 PM »
You have to supply a size for you payload buffer.
Just do
char payload[16]; or whatever you think you need to hold your longest string.

Don't add the * to your definition.
using payload by itself is affectively the pointer to the array (buffer).


P.S. wrap your code section in code tags... I'll edit your post so you can look at the source to see how it is done.

 

anything