Solved

atoi again

Posted on 2004-08-07
4
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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