Solved

atoi again

Posted on 2004-08-07
4
458 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ignoring whitespace is pretty easy ... just put in a if()
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now