Solved

atoi again

Posted on 2004-08-07
4
461 Views
Last Modified: 2010-04-15
Hello all

I'm trying to do the equivalent of the easy C code here
int someNum = atoi( someArray[20] );
Where someArray is an array of characters.

You would think that this would work:

someArray[20] = '9';
int someNum = Convert.ToInt32(someArray[20]);
//but someNum = 57 since ASCII 9 = 57.

So I am using this:
int arm2 = Convert.ToInt32(new string(rec,84,1));

Is there an easier (faster) way of doing this?  I am doing some reading/writing ascii files that reads in a line and pulls numbers from it using these converts.  I am trying to cut down on the time it takes for it to read all the stuff from the file.

Regards,
NTAC
0
Comment
Question by:NTAC
  • 2
4 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 11745528
Unless you have to have a character array, why not use StreamReader to read the file into a strings.




0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
ID: 11745641
int val;
for(i-=0;i<someArray.Length;i++) {
    val = val * 10 + (int) someArray[i];
}

this will work on a char array of appropriate size (simple modification to support whatever sized array)

string foo = new string(someArray);
int bar = int.Parse(foo);

works as well ...
0
 
LVL 4

Author Comment

by:NTAC
ID: 11746224
cookre:  the problem with that is the file is over 1Gb in size, and the performance between StreamReader vs BufferedStream(using a FileStream) is significant.  I think it has to do with the overhead of the strings.  

greg:  the reason I didn't use my own atoi function is whitespaces.  Sometimes there are whitespaces and the c version of atoi ignores them--but I didn't put it in my function.  I'll do some performance comparisions and see what happens.

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11746963
ignoring whitespace is pretty easy ... just put in a if()
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 31
Coding C# in Linux 8 72
Finally almost got it 5 38
How to parse the string and split the sub directory names in asp.net 7 44
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question