Helo!
I'm new here. I need to convert byte in char* and I don't know how.
Actualy, I didn't find any information what is "char*".
I'm trying to send byte by MQTT, but parameters of this function must be char*.
Thank you in advance!
Normally, char* is a string (or an array of char).
char* is a pointer to a character, that is, it points to data that exists somewhere else, but doesn't actually contain the string.
As for the conversion I'm not sure, I don't think I've ever done something like that, or at least not for a while.
Usually to cast a byte to a char you simply put (char) in front of it. They're both 8 bits wide so there's no issue.
Post your code, it'll be obvious to anyone that knows the language how to sort it out.
MrDropsy:
char* is a pointer to a character, that is, it points to data that exists somewhere else, but doesn't actually contain the string.As for the conversion I'm not sure, I don't think I've ever done something like that, or at least not for a while.
It's strange because when I tried to send char
client.publish("deltaPumpColector",char(deltaPumpColector));
I got : invalid conversion from 'char" to 'char*'.
KenF:
Tryclient.publish("deltaPumpColector",deltaPumpColector);
Otherwise Check here
I'm getting : Invalid conversion from 'byte' to 'char*'
I haven't a clue what client.publish does,
I haven't a clue what deltaPumpColector holds.
If this doesn't work, we're just PITW
client.publish("deltaPumpColector",(char *) deltaPumpColector);
KenF:
I haven't a clue what client.publish does,
I haven't a clue what deltaPumpColector holds.If this doesn't work, we're just PITW
client.publish("deltaPumpColector",(char *) deltaPumpColector);
Actually this was compiled, but now I will test if will work properly.
No! It doesn't work. I'm getting blank messages.
deltaPumpColector holding bytes.
client.publish() is function from MQTT Library - Arduino Client for MQTT · knolleary
It sending messages by MQTT.
What if you do something like:
client.publish("deltaPumpColector","Hello, this my message about deltaPumpColector.");
Or even something like:
deltaPumpColector[] = "Hello this is my message about deltaPumpColector."
client.publish("deltaPumpColector", deltaPumpColector);
What happens?
luisilva:
What if you do something like:client.publish("deltaPumpColector","Hello, this my message about deltaPumpColector.");
Or even something like:
deltaPumpColector[] = "Hello this is my message about deltaPumpColector."
client.publish("deltaPumpColector", deltaPumpColector);
What happens?
both of them works.
char deltaPumpColector1[] = "Hello this is my message about deltaPumpColector.";
client.publish("deltaPumpColector", deltaPumpColector1);
So, what is the "deltaPumpColector" of your code? Can you with this examples make your code work?
luisilva:
So, what is the "deltaPumpColector" of your code? Can you with this examples make your code work?
deltaPumpColector is variable which can get value from 1 to 6 and I decided to be byte for memory saving.
I read and tried many things by no success.
If the function rerquires a char pointer, then you give it the address of the char ( or byte ) that you are interested in. Like this.
void funcNeedsChar( char* c ) ;
,,,
char c = 'A' ;
funcNeedsChar( &c ) ;
michinyon:
If the function rerquires a char pointer, then you give it the address of the char ( or byte ) that you are interested in. Like this.void funcNeedsChar( char* c ) ;
,,,
char c = 'A' ;
funcNeedsChar( &c ) ;
I tried :
client.publish("deltaPumpColector",&deltaPumpColector);
And I got :
mqtt_basic_DHT22_RTD_Eprom.ino: In function 'void loop()':
mqtt_basic_DHT22_RTD_Eprom:122: error: invalid conversion from 'byte*' to 'char*'
mqtt_basic_DHT22_RTD_Eprom:122: error: initializing argument 2 of 'boolean PubSubClient::publish(char*, char*)'
The right way may be is to convert The byte to char by ASCII table because when I do it like that:
char test = char(deltaPumpColector);
char deltaPumpColector1[2];
deltaPumpColector1[1]= test;
client.publish("deltaPumpColector", deltaPumpColector1);
I'm getting nonsense symbols.
Barbarian_:
deltaPumpColector is variable which can get value from 1 to 6 and I decided to be byte for memory saving.
(...)
byte and char have the same lenght (8 bits=1 byte).
In thins case you can do:
client.publish("deltaPumpColector", (char*) &deltaPumpColector);
but the function is expecting a string (i.e. text) if you only provide it with a character it will not work properly. What are you trying to do anyway? What is your complete code?
EDIT: at the first time I don't read this part of your post:
Barbarian_:
(...)
I'm getting nonsense symbols.
This is normal and is what I tried to say with "it will not work properly". You need to pass to the function some text (a string). If you are trying to send only one number, then the suggestion of Delta_G will be the best.