I kind of new to C++, so I'm not sure how to perform such casting.
Example:
long myLong = 12345L;
convertToCharArray(myLong); //returns an array with the digits of myLong
BTW I'm working with Arduino IDE.
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityAs with many things in C there are many ways of skinning this particular cat.
All of them rely on you first having made a character array to store the result in - you can't "return" an array from a function.
The simplest, though non-standard, way is to use ltoa()
, which is an avr-libc specific function (not part of the standard libc functions):
long myLong = 12345L;
char myCharArray[10]; // Enough room for the answer + 1 NULL char
ltoa(myLong, myCharArray, 10);
The 10
in that function call defines the base to represent the number in - base 10 in this case: decimal.
Another way, which is more portable, is to format the string using snprintf()
:
snprintf(myCharArray, 10, "%dl", myLong);
In this case the 10
is the maximum length of string to prevent it overrunning the 10-character array we made.
A pure Arduino way of doing it would be to use the dreaded String
class:
String myString = String(myLong);
myString.toCharArray(myString, 10);
Again in this example the 10
is the length of the buffer we are copying in to.
And there are more. You could do it manually, character by character using division and modulus operators, for example.
This is a job for pointers.
long myLong = 12345;
char* myCharArray = (char*)&myLong;
Now pointers and arrays work the same. So you can now use myCharArray[0] to refer to the first byte and myCharArray[1] for the second etc.
You might be looking for a function that returns the textual representation of a long number. In that case there is actually an AVR standard library function (avr-libc:stdlib.h ltoa):
const size_t BUF_MAX = 32;
char buf[BUF_MAX];
const int RADIX = 10;
long myLong = 12345L;
ltoa(myLong, buf, RADIX);
The buffer, buf
, will contain the textual representation of the number, myLong
, in the given RADIX
.
Cheers!
Well for such situations I did found a solution.
To convert Float or Long or int into character array format, we could use dtostrf()
.
Syntax:
dtostrf(Source_var, StringLength, numVarsAfterDecimal,destinaton_var)
Source_var = the variable you want to convert (float/int/long)
StringLength = Length of the string (for your case just keep it 1)
numVarsAfterDecimal = required number of values after decimal point(keep it zero for now)
destination_var = the char variable to where the value must be stored(do not use square brackets).
For converting Long to array follow the below code
long myLong=12345L;
char var[5];
dtostrf(myLong,1,0,var);
If all goes well
Result will be like var[]={'1','2','3','4','5'}
I found this function very useful and it had been working great for me. Hope it does the same for you.
For more info on dtostrf() : Link
dtostrf()
, you are implicitly converting the number to float
. This is expensive, unneeded, and looses precision if the number is larger than 2^24.
Jul 15, 2017 at 12:10
I kind of new to C++, so I'm not sure how to perform such casting.
to recast it, just make a char pointer and pointer it to the variable. Fairly simple.
if you want to convert ti ascii, there are numerous ways, each with their own pros and cons.
I wrote a set of conversion routines here: https://dannyelectronics.wordpress.com/2017/07/04/a-very-fast-numeric-to-ascii-conversion-routine/
they are designed to convert 8 digit unsigned long to ascii but can be easily rewrite to convert 5 digit.