Quick way to convert int to array of numbers

J

John B

Hi all,
Say I have the int 123456789
What would be the quickest/best way to convert it to
int[]{1,2,3,4,5,6,7,8,9}

What I came up with was to determine the largest factor of 10 (100M)
divide by that, truncate decimals and that'd be the 1st number, subtract
that number multiplied by the current factor, next smallest factor etc..

But it seems very roundabout, surely there'd be a better way :)

TIA

JB
 
J

John B

Paul said:
John B said:
Say I have the int 123456789
What would be the quickest/best way to convert it to
int[]{1,2,3,4,5,6,7,8,9}

I think this is the most readable way:

int[] x = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
x = Convert.ToInt32(s);
}

Unless this is really a performance bottleneck, there's no need to do
anything cleverer.



Yep, except its an actual long so I just convert with ToString to begin
with.
Its not at all a bottleneck but its very strange as I have had one
instance where I got an "Input string was not in a correct format" on
int.Parse

The actual code is

//value is a long
string sVal = value.ToString();
for (int i = sVal.Length - 1; i >= 0; i--)
{
int iVal = int.Parse(sVal.ToString()); //Here is where the
exception was raised.
//...Do some validation
}

So I thought it'd just be easier to not convert them to a string and
just treat them as int all the time.
Of course this happened at a client site and I cannot reproduce it here :)

Cheers

JB
 
A

Arne Vajhøj

John said:
Say I have the int 123456789
What would be the quickest/best way to convert it to
int[]{1,2,3,4,5,6,7,8,9}

What I came up with was to determine the largest factor of 10 (100M)
divide by that, truncate decimals and that'd be the 1st number, subtract
that number multiplied by the current factor, next smallest factor etc..

But it seems very roundabout, surely there'd be a better way :)

I would say that it is easier/more readable to do:

for(int i = 0; i < n; i++)
{
digs[n-i-1] = tmp % 10;
tmp /= 10;
}

Arne
 
J

John B

Arne said:
John said:
Say I have the int 123456789
What would be the quickest/best way to convert it to
int[]{1,2,3,4,5,6,7,8,9}

What I came up with was to determine the largest factor of 10 (100M)
divide by that, truncate decimals and that'd be the 1st number,
subtract that number multiplied by the current factor, next smallest
factor etc..

But it seems very roundabout, surely there'd be a better way :)

I would say that it is easier/more readable to do:

for(int i = 0; i < n; i++)
{
digs[n-i-1] = tmp % 10;
tmp /= 10;
}

Arne
Perfect Arne, thanks.
Actual code

long value = 123456765467864589;
List<byte> values = new List<byte>();
long tmp = value;
while (tmp != 0)
{
values.Add((byte)(tmp % 10));
tmp /= 10;
}

And then I can just iterate through in reverse order and all is good.
Thanks again ;)

JB
 
J

John B

Peter said:
Those should only apply to floating point values. Assuming he started
with an int or a long, that shouldn't have happened.

Taking as granted that it did happen, I guess that suggests that the
actual code was wrong. For sure, in theory code that starts with an
int, converts to a string, and parses each character individually to
convert back to an array of ints, should never fail.

Of course, without seeing his actual code that failed, it's not possible
to say what was actually wrong with it. Assuming the code he posted is
in fact the actual code, or at least reasonably close to it, I think *****
it's more likely that he had a negative number and the conversion failed
on the minus sign. That'd be easy enough to deal with, but of course it
******
<....>
Thanks Peter, that must've been it.
I have now added a check to ensure that negative numbers are flagged as
invalid to prevent this from happening again.

The actual code is just for validating the IMEI of a phone.

Thanks,

JB
 
B

Ben Voigt [C++ MVP]

John said:
******
<....>
Thanks Peter, that must've been it.
I have now added a check to ensure that negative numbers are flagged
as invalid to prevent this from happening again.

The actual code is just for validating the IMEI of a phone.

That's probably long enough to overflow int.
 
B

Ben Voigt [C++ MVP]

John said:
Paul said:
John B said:
Say I have the int 123456789
What would be the quickest/best way to convert it to
int[]{1,2,3,4,5,6,7,8,9}

I think this is the most readable way:

int[] x = new int[s.Length];
for (int i = 0; i < s.Length; i++)
{
x = Convert.ToInt32(s);
}

Unless this is really a performance bottleneck, there's no need to do
anything cleverer.



Yep, except its an actual long so I just convert with ToString to
begin with.
Its not at all a bottleneck but its very strange as I have had one
instance where I got an "Input string was not in a correct format" on
int.Parse


Skip the Parse step, it's actually easier to convert the raw characters than
the integer.

int[] x = new int[s.Length];
int i = 0;
foreach (char c in s)
x[i++] = c - '0';
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top