From Long variable to array of ascii code

If I have a long variable like:

Long testVar = "125" // The longitude is variable

How can I get a char array with the ASCII code equivalent of each number of testVar?

So the output should be something like:

outputVar[0] = 49   //ascii equivalent of 1
outputVar[1] = 50   //ascii equivalent of 2
outputVar[2] = 53   //ascii equivalent of 5

Ideally the output of outputVar inside a For cycle.

Long testVar = "125" // The longitude is variable

It doesn't make sense to assign "125" (a character string) to a long.

It doesn't make sense to use a comment in place of a ;.

You can convert a long that contains a valid value to a string using sprintf() or one of the n-type variants.

Long testVar = "125" // The longitude is variable

"125" is a string and is NOT the same as 125! Try

char testVar = "125";


I made some mistakes in my original code, the testVar is actually a Long var, the " are not in the original code. So:

Long testVar = 125 // The longitude is variable

I understand 125 is not long, but this is only an example, thats why i wrote "The longitude is variable", some times it could be 125 and sometimes a greater value like 1245675753.

I know i can just simply char testVar = "125"; but the thing is I cant assign the value, the variable is already created, so I need to convert that long value to the ascii char array.

Then use sprintf.


Then use sprintf.


Can you please make an example? Since i was using this king of approach but I cant get it to work right:

Long testVar = 123456;
char _buffer[50];
sprintf(_buffer,  "%d", testVar);

int test = _buffer[0] - 0;   //this should print the ASCII equivalent of 1 but is printing something else

int test = _buffer[0] - 0;

should read

int test = _buffer[0] - '0';

The ascii chars for the numbers are in order from 0 to 9 So if you subtract the value of the char '0' you get the chars numeric value.


sprintf(_buffer,  "%d", testVar);

You have to use the correct format specifier. %d is the format specifier for ints, not longs. %ld is the format specifier for longs.

It may actually be legal, but I would avoid using "Long". C++ keywords are case-sensitive.