how to put a integer into an array using modulo math?

I know I saw a good example somewhere on how to step through an integer and place each digit into an array, but I can't seem to find it now.

for example:

data = 98;

I want the array to look like char dataArray[] = {9,8,0}

I want to use the modulo math method, but I am confused as to how to do it.

itoa()

I'm looking for how to do it using modulo math, but thanks

General idea for a three digit number:

dataArray[0]=(x/100)+0x30;
dataArray[1]=((x%100)/10)+0x30;
dataArray[2]=(x%10)+0x30;

The +0x30 is a trick to avoid having to call itoa() to convert each digit from an int to a char - the digits 0~9 are ASCII codes 0x30~0x39 - so you can just add 0x30 to the digit to get the ascii character for it.

Perhaps it would help if you tell us what you think "modulo math" is or looks like. My maths teacher friends have never heard of it.

Are you perhaps thinking of repeated divides by 10 ? Though I can't see how you turn 98 into (9, 8, 0). Where's the zero come from?

Steve

I think DrAzzy's approach is what I am looking for. Modulo math simply gives the remainder of a division as the result:

10%10=0
11%10=1

Thanks

slipstick:
Perhaps it would help if you tell us what you think "modulo math" is or looks like. My maths teacher friends have never heard of it.

I guess they never took any course in discrete math.

amdkt7:
I'm looking for how to do it using modulo math, but thanks

So it's a school assignment, then?

aarg:
I guess they never took any course in discrete math.
Discrete mathematics - Wikipedia

Or maybe they've just never heard the term "modulo math". A term which incidentally doesn't appear on that page or anywhere else in Wikipedia. One did ask if I meant modular arithmetic and I didn't know. I still don't.

Steve

No, I'm learning on my own. You maybe right that itoa() is an easier way to do it, but since a number of people have suggested the modulo method I wanted to learn how that works. I find it best to stay focused on one thing until I get it, then try other methods. I asked for a specific method because I wanted to learn it.

I am still not clear what you want to do.

I want the array to look like char dataArray[] = {9,8,0}

Should the entries in the array be numbers as you have written or characters representing numbers ?

slipstick:
Or maybe they've just never heard the term "modulo math". A term which incidentally doesn't appear on that page or anywhere else in Wikipedia. One did ask if I meant modular arithmetic and I didn't know. I still don't.

Steve

Modular arithmetic is a subset of discrete math.

The modulo method will produce the digits in reverse order.

while(X != 0) {
Serial.print(X % 10); // print the units digit
X = X/10;
}

Note that this will do odd things with negative numbers and with zero.

UKHeliBob:
I am still not clear what you want to do.
Should the entries in the array be numbers as you have written or characters representing numbers ?

That's what threw me. The last number is a zero, so it looks like an attempt to make the cstring "98". But yes, it's not ASCII as shown.

See #3. The last char is meant to be a null terminator I'll bet, since the op specified a char array.

boolrules:
See #3. The last char is meant to be a null terminator I'll bet, since the op specified a char array.

But that would give you a string " "
:wink:

Ah, yes, I was mixing my char and integer values in my example, I was meaning to show the null at the end of the array.

I got some good information from everyone, I'll spend some more time playing with it today.

The +0x30 is a trick to avoid having to call itoa() to convert each digit from an int to a char - the digits 0~9 are ASCII codes 0x30~0x39 - so you can just add 0x30 to the digit to get the ascii character for it.

It would certainly be simpler, and, in my mind, far more intuitive, to use '0' instead of 0x30 or 48.