473,506 Members | 17,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick way to convert int to array of numbers

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
Jun 27 '08 #1
9 1474
"John B" <jb******@yahoo.comwrote:
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[i] = Convert.ToInt32(s[i]);
}

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

Eq.
Jun 27 '08 #2
Paul E Collins wrote:
"John B" <jb******@yahoo.comwrote:
>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[i] = Convert.ToInt32(s[i]);
}

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[i].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
Jun 27 '08 #3
John B wrote:
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
Jun 27 '08 #4
"John B" <jb******@yahoo.comwrote:
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
You probably had "NaN" (zero divided by zero) or "Infinity" (anything
else divided by zero). You won't be able to convert those strings to an
integer.

Eq.
Jun 27 '08 #5
On Wed, 30 Apr 2008 19:31:26 -0700, Paul E Collins
<fi******************@CL4.orgwrote:
"John B" <jb******@yahoo.comwrote:
>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

You probably had "NaN" (zero divided by zero) or "Infinity" (anything
else divided by zero). You won't be able to convert those strings to an
integer.
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 begs the
question as to what the actual result in his code should be.

Pete
Jun 27 '08 #6
Peter Duniho wrote:
On Wed, 30 Apr 2008 19:31:26 -0700, Paul E Collins
<fi******************@CL4.orgwrote:
>"John B" <jb******@yahoo.comwrote:
>>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

You probably had "NaN" (zero divided by zero) or "Infinity" (anything
else divided by zero). You won't be able to convert those strings to an
integer.

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
Jun 27 '08 #7
John B wrote:
Peter Duniho wrote:
>On Wed, 30 Apr 2008 19:31:26 -0700, Paul E Collins
<fi******************@CL4.orgwrote:
>>"John B" <jb******@yahoo.comwrote:

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

You probably had "NaN" (zero divided by zero) or "Infinity"
(anything else divided by zero). You won't be able to convert those
strings to an integer.

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.
That's probably long enough to overflow int.
>
Thanks,

JB

Jun 27 '08 #8
John B wrote:
Paul E Collins wrote:
>"John B" <jb******@yahoo.comwrote:
>>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[i] = Convert.ToInt32(s[i]);
}

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';
Jun 27 '08 #9
On Thu, 01 May 2008 06:37:31 -0700, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
[...]
>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.
He did write, in a more recent post, that his input was typed as long.
Though, he could be mistaken and somewhere along the line it's actually an
int. That would certainly explain why the number wound up negative at
some point.

Pete
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.