1

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.

5 Answers 5

3

As 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.

0

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.

5
  • My phone won't let me edit that to fix the code proper. If someone wouldn't mind editing that for me I would greatly appreciate it.
    – Delta_G
    Jul 14, 2017 at 19:12
  • I think he means he wants an ASCII representation of the number, not the raw bytes.
    – Majenko
    Jul 14, 2017 at 19:23
  • You're right. ASCII representation. Jul 14, 2017 at 19:32
  • 1
    Well he referred to "casting" in the question so I told him how to cast it. If you just want ascii use atol.
    – Delta_G
    Jul 14, 2017 at 19:50
  • I really meant ltoa.
    – Delta_G
    Jul 14, 2017 at 20:54
0

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!

2
  • ltoa isn't standard. It's pretty unique to avr-libc.
    – Majenko
    Jul 14, 2017 at 20:00
  • Let me fix that for your :) Jul 14, 2017 at 20:03
0

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

1
  • By using 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.