data types

Hi guys, Sorry for the newbie question but I'm really stuck at this..

I got a byte value, I can tell print out to format it whatever I want:

byte value; Serial.print(value, DEC); //or HEX or BIN

How can I do the same if it was to be saved as a String? thanks.

How can I do the same if it was to be saved as a String?

You performed some conversion from a binary value to a numeric representation when you created the String. You can't print that text in the String in any other format, later.

It is suggested not to use String but char[].

Define the variable as

char test[xyz]; // (where xyz is the length of the longest item to display plus 1)

I believe


will work.

thanks for the replies, however I might have not explain myself quite well

I want this value to be always output as a string of char but representing different data types i.e.

byte value; char test[8];

//byte as BIN Serial.print(test);

//byte as DEC Serial.print(test);

//byte as CHAR Serial.print(test);


will always print what is in the string. If you want it in a different format then you have to put that in the string yourself.

Why do you want/need the variable always to be a string ? Can you give an example of this requirement ?

What do you mean byte as bin or byte as hex. The byte is ALWAYS stored in binary. That's the only type of number a computer ever deals with. You can write the number in your code a bunch of different ways but it doesn't change the underlying value.

If what you are asking for is how to convert a numeric value to a string, then you should learn about sprintf

byte value = 123;
char str[9]; //Must be able to hold zero termination
sprintf(str, "%d", value);
//str is now a null-terminated string containing "123"

EDIT: Removed memset, not necessary.

You can also use itoa() to convert the value to a string, in whatever base you want. Which, I suspect, was the real question to begin with. Not that it is clear WHY you need the value in a string in any particular base.