# XNA reading from files: ints turn into ASCII

## Recommended Posts

Hi people, hopefully a really simple problem to solve. Im used to c++ style use of ifstreams and ofstreams and having problems getting my head around reading ints from a text file successfully in XNA. At the moment the values I'm reading in from a text file such as the following 10 5 16 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 Seem to be read in as ASCII values when going through the debugger. The relevent code that I'm using is
public Level(String name)
{
TYPE tileType;

.
.
.
}

I'm obviously going about something the wrong way, the 10 is being read as 52, or some similar value and so on. Please help! If this is the biggest boneheaded thing ever and there is a useful article on streams in c# for c++ zealots let me know and please share. Thanks

##### Share on other sites
The problem you have is that the text file just stores ascii data, and as far as I can tell the Stream class just gives you the raw data from the file. Im not a c# programmer but a quick google search gave me:

int.Parse(<string>)

which could be what you need to turn the ASCII data in the text file into actual numbers.

##### Share on other sites
I'd personally be inclined to read a line at a time with StreamReader.ReadLine(), use string.Split(' ') to get the individual numbers as strings, then int.Parse() to turn each of these numeric strings into integers.

I'm quite fond of the Array.ConvertAll() method, which takes an input array and applies a user-defined converter to return a new array.

var line = levelFile.ReadLine();var numbers = Array.ConvertAll<string, int>(line.Split(' '), int.Parse);// alternatively, use a lambda:var numbers = Array.ConvertAll(line.Split(' '), n => int.Parse(n));

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982187

• 9
• 24
• 9
• 9
• 13