Help with converting integer to unsigned char*

The library for 12864 LCD I am using requires the message to be displayed should be unsigned char* type, I am new to C++, could anyone tell me how to convert the integer or float to unsigned char*?

Thanks a lot.

Ardypro

The easiest way is to cast the value when you call the function, provided you know the library is simply expecting a buffer with the value and not actually chars. Cast by putting the desired type in front of the variable name.

myFunction((char *) myVarName)

That's not going to work if "myVarName" is a float!

OP, most display libraries have methods for displaying common datatypes - which one are you using?
Can you post your code?

I really also think that OP needs to clarify if he needs to convert the numbers to a string or if the value is just passed in what is effectively a byte buffer.

For integer:

char temp[20];
int myNumber = 3822;

sprintf(temp,"%d",myNumber);
LCD.whateverPrintFunctionItIs(temp);

Floats are slightly different because the Arduino sprintf function doesn't have them implemented. Instead, you use dtostrf():

char temp[20];
float myNumber = 3822.286455;

dtostrf(myNumber,0,4,temp);
LCD.whateverPrintFunctionItIs(temp);

In both cases you have to ensure that the temp variable is big enough to hold the result plus one character for a terminating NULL character (so, for 6 digits, you need a minimum of 7 array slices.)

The dtostrf() function has 2 numbers in it - 0 and 4. The 0 is the minimum size of the output (leave it at 0), and the 4 is the number of decimal places to display.

AWOL:
That's not going to work if "myVarName" is a float!

OP, most display libraries have methods for displaying common datatypes - which one are you using?
Can you post your code?

The library LCD12864RSPI is supposed to be programmed by dfrobot, the function to display string is declared as void LCD12864RSPI::DisplayString(int X,int Y,uchar *ptr,int dat), the last param is the length of the *ptr. There is no overrided method to display string with string as its parameter. :frowning:

There is no overrided method to display string with string as its parameter

A char* can be a pointer to a C string, which is different to a String.
It isn't clear what you want.

majenko:
For integer:

char temp[20];

int myNumber = 3822;

sprintf(temp,"%d",myNumber);
LCD.whateverPrintFunctionItIs(temp);




...

Thank you, but this code does not work, since the function in the library requires the temp[20] should be unsigned char*.

char temp[20];
int myNumber = 3822;

sprintf(temp,"%d",myNumber);
LCD.whateverPrintFunctionItIs((unsigned char*)temp);

Internally there is no difference at all between a char * and an unsigned char * - it purely defines how the 7th bit is interpreted for maths operations. Just casting the pointer to (unsigned char *) is sufficient to get around the signed/unsigned problem.

Alternate

unsigned char temp[20];
int myNumber = 3822;

sprintf((char*)temp,"%d",myNumber);
LCD.whateverPrintFunctionItIs(temp);

I made a demo function to convert the integer to unsigned char*, the result in Serial monitor seemed correct, but when to be displayed with LCD, it did not work as expected.

unsigned char* int2uCharArray(int value)
{
  char buffer[5]={    '0'            };
  unsigned char ubuffer[5]={    '0'            };
  itoa(value,buffer,10);
  for (int i=0;i<5;i++)
    ubuffer[i]=buffer[i];

  Serial.println("int2chararray");
  for (int i=0;i<5;i++)
    Serial.println(ubuffer[i]);
  Serial.println("END TEST");

  return ubuffer;
}

Never, ever return a pointer to an automatic variable.
"ubuffer" is allocated on the stack, so becomes free for anyone else to stomp on as soon as your function returns.

If this is the function you are using you cannot just return ubuffer and use it as it is only valid in the function. Outside of the function the value is not defined and will probably be corrupted. You need to add the word static in front of the variable declaration for this to work.

  char buffer[5]={    '0'            };
  unsigned char ubuffer[5]={    '0'            };

So, the other 4 elements of each array are undefined. Why is that?

Let's also hope that "value" never gets bigger than 9999 and/or negative.

PaulS:

  char buffer[5]={    '0'            };

unsigned char ubuffer[5]={    ‘0’            };



So, the other 4 elements of each array are undefined. Why is that?

Thanks, Paul, it should be “00000” instead.

And why the two buffers?

AWOL:
Never, ever return a pointer to an automatic variable.
"ubuffer" is allocated on the stack, so becomes free for anyone else to stomp on as soon as your function returns.

should this be replaced as Int2uCharArr(int value, unsigned char &output) ?

should this be replaced as Int2uCharArr(int value, unsigned char &output) ?

If you want to write to just one character, yes. Otherwise, you need a pointer or an array as the reference argument.