Unsigned Long to Array

Hello everyone,

This is a very simple request,
with an Arduino UNO,

Is it possible to convert each number of a unsigned long to an array

From:
unsigned long var = 4294967295;

To:
array[0] = 4
array[1] = 2
array[2] = 9
array[3] = 4
array[4] = 9
array[5] = 6
array[6] = 7
array[7] = 2
array[8] = 9
array[9] = 5

Thanks

Yes. Here is one way to do it

unsigned long var1 = 4294967295;
byte numbers[10];

void setup() {
  Serial.begin(115200);
  convertUlToArray(var);
  printResults();
}

void loop() {
}

void convertUlToArray(unsigned long input) {
  for (int i = 9; i >= 0; i--) {
    numbers[i] = input % 10;
    input = input / 10;
  }
}

void printResults() {
  Serial.print(F("Unsigned long  = "));
  Serial.println(var);
  Serial.print(F("Array contents = "));

  for (byte i = 0; i < 10; i++) {
    Serial.print(numbers[i]);
  }
  Serial.println();
}

Serial monitor output

Unsigned long  = 4294967295
Array contents = 4294967295
  • In the end, what are you trying to.
1 Like

Many thanks Dave
I had to edit 'var' to 'var1'
it's working fine.
since there's no array function to convert long, double, etc..
this will work

unsigned long var1 = 4294967295;
byte numbers[10];

void convertUlToArray(unsigned long input) {
  for (int i = 9; i >= 0; i--) {
    numbers[i] = input % 10;
    input = input / 10;
  }

Hello LarryD,
I was looking for a way to convert long,double etc.
since the array sample code from Arduino can't do this.
Thanks

Why do you need this ?
Are you trying to get a text representation?

The modulo approach won’t work for a double and nor sure this is what you need for a negative number

my mistake just for long.
thanks

Dave gave me the right answer for long var,

Thanks again Dave.

Sorry, after I ran it, I started editing the sketch to print out a few different examples of values in unsigned longs, with var1, var2, etc. Then decided not to bother doing that, but pasted the edited code which I obviously didn't compile before posting it.

Ah, I just did something that worked for unsigned long. I didn't know you also wanted to convert other numeric types. Perhaps try converting to a String and then step along the characters in the string to get the number corresponding to each character.

Hello Dave,
you did an excellent job, I'm a very beginner to Arduino and I did mention double,
but for what I'm working now the Long, will do the work fine.

Unsigned ? (As per the title)

I’m curious why you need this

Hello J-M-L
for now I'm learning Arduino code, and read about array in general
I dont have a specific project just learning.
I could have ask for 'int' instead.
Have a great day.

An int is like a long, it’s signed so what do you expect in your array for -123456 ?

Hello Dave,
I tried to find info's about this line 'void convertUlToArray(unsigned long input)'
did not find anything about 'input' or function related.
if possible, can you direct me to a source that I can learn from it.
Thanks again Dave.

Just found :
(C++ Function (With Examples))

Yes, it's a function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.