Concatenate Serial.print() values

I bought an lcd from sparkfun and am using it to make a primitive game system. The thing is, the controller I'm using outputs a decimal number, and the lcd talks in hex. I managed to convert the numbers to hex all right, but my function creates two separate 'char' variables. Unfortunately, it won't work when I send this:

Serial.print('0x',BYTE);
Serial.print(myvar[0],BYTE);
Serial.print(myvar[1],BYTE);

So is there a way to concatenate the value you send into one string?

but my function creates two separate 'char' variables

So, write the two character into the appropriate places in a string.

Crudly:

char buffer [] ="0x..";
...
buffer [2] = msbInASCII;
buffer [3] = lsbInASCII;

nasty, dirty, but should work.

Alternately you could use sprintf;

char mystr[4];

sprintf(mystr,"0x%c%c",myvar[0],myvar[1]);

Groove's answer would be much faster and more efficient though.

I already put the values into an array like Groove said, but my question was not how to contain the values together, but rather how to print all of those values in the same Serial.print() command. It's because my LCD doesn't understand if the values are sent separately, like did in my first example. I'm also sure it won't work if I do Serial.print(myvar[0-3], BYTE) because it would think I'm trying to say Serial.print(myvar[-3],BYTE).

Serial.print (buffer);

Well, I tried the code and it didn't work. Can anyone give me an explanation as to why? I highlighted the stuff that is relevant to the question.

[glow]#define TO_HEX(x)  (((x) > 10) ? (x) - 10 + 'A' : (x) + '0')
byte xVal = 64;
byte yVal = 32;
char xValHex[4];
sprintf(xValHex,'0x%c%c', TO_HEX (xVal >> 4), TO_HEX (xVal & 15));
char yValHex[4];
sprintf(yValHex,'0x%c%c', TO_HEX (yVal >> 4), TO_HEX (yVal & 15));[/glow]

void setup(){
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x02,BYTE);//backlight
Serial.print(0x19,BYTE);//set to 25
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x03,BYTE);//circle
[glow]Serial.print(xValHex,BYTE);//x-coord
Serial.print(yValHex,BYTE);//y-coord[/glow]
Serial.print(0x0A,BYTE);//radius ten
Serial.print(0x01,BYTE);//draw

It gives me an error that says "constructor, destructor, or type conversion before '(' token."

Executable code has to be inside a function.
Those sprintfs are orphans.
(Does anyone know if the %#x template works with the arduino's sprintf?)

It looks like arduino sprintf does work with hex values.

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));

}

void loop() {
  int num = random(0,99);
  char foo[10];

  sprintf(foo,"Int: %i Hex: %x",num,num);
  Serial.println(foo);
  
  delay(1000);
}

Output:

Int: 89 Hex: 59

Int: 93 Hex: 5d

Int: 56 Hex: 38

Int: 3 Hex: 3

Int: 86 Hex: 56

Int: 26 Hex: 1a

It looks like arduino sprintf does work with hex values

Imagine that. ::slight_smile:

Thank you for that. Unfortunately, my compiler is still being a pile of crap. This time, is says "invalid conversion from 'int' to 'cost char*.' Does anyone know how to fix this? Here's the code:

#define TO_HEX(x)  (((x) > 10) ? (x) - 10 + 'A' : (x) + '0')
byte xVal = 64;
byte yVal = 32;
char xValHex[4];
char yValHex[4];

void setup(){
sprintf(yValHex,'0x%c%c', TO_HEX (yVal >> 4), TO_HEX (yVal & 15));
sprintf(xValHex,'0x%c%c', TO_HEX (xVal >> 4), TO_HEX (xVal & 15));
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x02,BYTE);//backlight
Serial.print(0x19,BYTE);//set to 25
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x03,BYTE);//circle
Serial.print(xValHex,BYTE);//x-coord
Serial.print(yValHex,BYTE);//y-coord
Serial.print(0x0A,BYTE);//radius ten
Serial.print(0x01,BYTE);//draw
}
void loop(){

}

I think the pile of crap is not the compiler.

Serial.print(xValHex,BYTE);//x-coord

All right. I knew I made a mistake somewhere, but I couldn't figure out what it was. I'm still somewhat new to Arduino and this project is slightly more advanced than I'm used to. Anyway, I can't seem to get it to work, so I need some non-crappy code that will get this thing up-and-running. Can anyone help... again? The code is the same as my previous post.

Serial.print(xValHex);//

Would that still print the same thing as the original code? I'm pretty sure it needs to because all the example code had the "BYTE" format on it.

sprintf(yValHex,[glow]"[/glow]0x%c%c[glow]"[/glow], TO_HEX (yVal >> 4), TO_HEX (yVal & 15));

I deleted the bad bit of code, but it still gave me the same error. In fact, the line it highlighted was not the Serial.print(), but the one with the sprintf(). Anyway, I am bothering you all once again to see if the great thinkers of Arduino can help out.

Was that the sprintf with the single quotes?
If not, could you repost?

The code is the same as the last time I posted my whole program, except the 'BYTE' Serial.print(xValHex,BYTE) and Serial.print(yValHex,BYTE) has been deleted.

If you won't repost, I'll assume the single quotes are still there.
Did you read reply #14?

Yes, the single quotes are still there. I assume that I need to chenge them to double quotes?