Solved

atoi again

Posted on 2004-08-07
4
463 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a simple RegEx to search for two words 27 123
Create a Dropdown list with sub group 10 42
Return array 3 33
.Net remove carriage returns, line feeds and tabbs 5 33
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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