Literal conversion from short or byte to char

Hello,

what I'm trying to do is pretty simple, but I've tried almost everything and I simply can't find the solution.

First step:
I have a short variable with the value 40 (example) and I want to convert it to a char (with ASCII code 40).

Second Step:
The second step is to concatenate various results to send via Serial (I really need to send the result in form of a String, since Ii'm using a library). This step is easy, but without the first step I cannot do it with sucess.

Many Thanks!

I have a short variable with the value 40 (example) and I want to convert it to a char (with ASCII code 40)

Any value 0 to 127 can bed directly assigned to a char without problem, whether, short or long.
(ASCII 40 is a '(' )

How can I do it?

I tried the follow with no success:

short var1 = 0;
short var2 = 16;
short var3 = 50;
short var4 = 6;
short var5 = 255;

String someFunction1()
{
return (String)var1+(String)var2+(String)var3+(String)var4+(String)var5;
}

String someFunction2()
{
return String(var1)+String(var2)+String(var3)+String(var4)+String(var5);
}

String someFunction3()
{
String message = "";
char test = var1;
message += test;
test = var2;
message += test;
test = var3;
message += test;
test = var4;
message += test;
return message;
}

Sorry, I've never used String ,but returning an automatic is never a good idea.

Try:

char randomVals[8];
randomVal[0] = var1;
randomVal[1] = var2;
randomVal[2] = var3;
//...

I will try that tomorrow :wink:

KeithRB:
Try:

char randomVals[8];
randomVal[0] = var1;
randomVal[1] = var2;
randomVal[2] = var3;
//...

That solution worked very well but there is something that I was strugling and I finally found out why. So here is the deal: A char array terminates with '\0', which as the ASCII code of 0. This means that everytime you convert from char array to String if a there's a value with 0 it will terminate the string. Everyone be careful when working with this.

Thanks!

This means that everytime you convert from char array to String if a there's a value with 0 it will terminate the string. Everyone be careful when working with this.

That's one of the reasons not to use the String class.

Of course, whatever it is you are doing probably should be done with byte arrays, not char arrays, so the array processing isn't halted by a NULL.

TGPTIAlves:
That solution worked very well but there is something that I was strugling and I finally found out why.

Which is why you should show all your code, and not just snippets.

PaulS:

This means that everytime you convert from char array to String if a there's a value with 0 it will terminate the string. Everyone be careful when working with this.

That's one of the reasons not to use the String class.

Of course, whatever it is you are doing probably should be done with byte arrays, not char arrays, so the array processing isn't halted by a NULL.

That problem doesn't exit with strings, only char arrays (the conversion from char array stops as soon as it finds a '\0' ASCII code 0). Yes, using a byte array might be a solution to this problem, but I still need to use a Sstring since I need to use a function that takes a String for argument.

Why do you "need" to use a String?

AWOL:
Why do you "need" to use a String?

I need to use a function from a library that takes a String for an argument.

I need to use a function from a library that takes a String for an argument.

What crappy library is that?

It's a 485 TDM serial communication protocol made by a friend which he only implemented a function to send information that takes a String from an argument.