从文件中读取 int 返回 ASCII
我目前正在制作一个游戏,但我似乎在从文本文件读取值时遇到问题。由于某种原因,当我读取该值时,它会给出该值的 ASCII 代码,而不是我将其写入文件时的实际值本身。我已经尝试了每个 ASCII 转换函数和字符串转换函数,但我似乎无法弄清楚。
我使用二维整数数组。我使用嵌套的 for 循环将每个元素写入文件中。我查看了该文件,值是正确的,但我不明白为什么它返回 ASCII 代码。这是我用来写入和读取文件的代码:
写入文件:
for (int i = 0; i < level.MaxRows(); i++)
{
for (int j = 0; j < level.MaxCols(); j++)
{
fileWrite.Write(level.GetValueAtIndex(i, j) + " ");
//Console.WriteLine(level.GetValueAtIndex(i, j));
}
//add new line
fileWrite.WriteLine();
}
这是我从文件中读取值的代码:
string str = "";
int iter = 0; //used to iterate in each column of array
for (int i = 0; i < level.MaxRows(); i++)
{
iter = 0;
//TODO: For some reason, the file is returning ASCII code, convert to int
//keep reading characters until a space is reached.
str = fileRead.ReadLine();
//take the above string and extract the values from it.
//Place each value in the level.
foreach (char id in str)
{
if (id != ' ')
{
//convert id to an int
num = (int)id;
level.ChangeTile(i, iter, num);
iter++;
}
}
这是我用来读取值的循环的最新版本。读取其他值也可以;只是当我到达阵列时,事情就出了问题。我想我的问题是,为什么要转换为 ASCII?如果我能弄清楚这一点,那么我也许就能解决这个问题。我正在使用 XNA 4 来制作我的游戏。
I'm currently making a game but I seem to have problems reading values from a text file. For some reason, when I read the value, it gives me the ASCII code of the value rather than the actual value itself when I wrote it to the file. I've tried about every ASCII conversion function and string conversion function, but I just can't seem to figure it out.
I use a 2D array of integers. I use a nested for loop to write each element into the file. I've looked at the file and the values are correct, but I don't understand why it's returning the ASCII code. Here's the code I'm using to write and read to file:
Writing to file:
for (int i = 0; i < level.MaxRows(); i++)
{
for (int j = 0; j < level.MaxCols(); j++)
{
fileWrite.Write(level.GetValueAtIndex(i, j) + " ");
//Console.WriteLine(level.GetValueAtIndex(i, j));
}
//add new line
fileWrite.WriteLine();
}
And here's the code where I read the values from the file:
string str = "";
int iter = 0; //used to iterate in each column of array
for (int i = 0; i < level.MaxRows(); i++)
{
iter = 0;
//TODO: For some reason, the file is returning ASCII code, convert to int
//keep reading characters until a space is reached.
str = fileRead.ReadLine();
//take the above string and extract the values from it.
//Place each value in the level.
foreach (char id in str)
{
if (id != ' ')
{
//convert id to an int
num = (int)id;
level.ChangeTile(i, iter, num);
iter++;
}
}
This is the latest version of the loop that I use to read the values. Reading other values is fine; it's just when I get to the array, things go wrong. I guess my question is, why did the conversion to ASCII happen? If I can figure that out, then I might be able to solve the issue. I'm using XNA 4 to make my game.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是转换为 ascii 的地方:
+ 运算符隐式地将 GetValueAtIndex 返回的整数转换为字符串,因为您要将其添加到字符串中(真的,您期望发生什么?)
此外,ReadLine 方法返回一个字符串,所以我不确定为什么你会期望一个数值神奇地回到这里。如果您想写入二进制数据,请查看 BinaryWriter
This is where the convertion to ascii is happening:
The + operator implicitly converts the integer returned by GetValueAtIndex into a string, because you are adding it to a string (really, what did you expect to happen?)
Furthermore, the ReadLine method returns a String, so I am not sure why you'd expect a numeric value to magically come back here. If you want to write binary data, look into BinaryWriter
您可以在此处将字符转换为字符代码:
id
变量是char
,将其转换为int
即可得到字符代码,而不是数值。此外,这会转换单个字符,而不是整数。例如,如果您的文本文件中有
"12 34 56 "
,它将获得 1、2、3、4、5 和 6 的代码,而不是 12、34 和 56。您会想要以空格分割行,并解析每个子字符串:
This is where you are converting the characters to character codes:
The
id
variable is achar
, and casting that toint
gives you the character code, not the numeric value.Also, this converts a single character, not a whole number. If you for example have
"12 34 56 "
in your text file, it will get the codes for 1, 2, 3, 4, 5 and 6, not 12, 34 and 56.You would want to split the line on spaces, and parse each substring:
更新:我保留了旧代码(如下),并假设每行都有一个记录,但我还添加了一种不同的方法,该方法应该可以处理一个记录上的多个整数行,用空格分隔。
一行上有多个记录
每行一个记录
请注意,如果您逐行写出每个整数(即您如何通过您在上面的代码中注释掉了 WriteLine)。如果您切换回使用 WriteLine,这应该可以工作。
Update: I've kept the old code (below) with the assumption that one record was on each line, but I've also added a different way of doing it that should work with multiple integers on a line, separated by a space.
Multiple records on one line
One record per line
Please note that the above should work if you write out each integer line-by-line (i.e. how you were doing it via the WriteLine which you remarked out in your code above). If you switch back to using a WriteLine, this should work.
你有:
char
是一个 ASCII 代码(或者可以被认为是这样的;从技术上讲,它是一个 unicode 代码点,但假设你正在编写 ANSI 代码,那么它是大致可比的或低值 UTF-8)。你想要的是:
You have:
A
char
is an ASCII code (or can be considered as such; technically it is a unicode code-point, but that is broadly comparable assuming you are writing ANSI or low-value UTF-8).What you want is:
此操作:
将从
level.GetValueAtIndex(i, j)
返回的int
转换为字符串。假设该函数对于特定的i
和j
返回值 5,那么您将“5”写入文件中。然后,当您读取时,它会被读取为由字符组成的字符串,并且当您将其简单地转换为
int
时,您会得到5
的 ASCII 代码。你需要的是:然而,只有当你只有个位数整数(只有 0 - 9)时,这才有效。这可能会更好:
This:
converts the
int
returned fromlevel.GetValueAtIndex(i, j)
into a string. Assuming the function returns the value 5 for a particulari
andj
then you write "5 " into the file.When you then read it is being read as a string which consists of chars and you get the ASCII code of
5
when you cast it simply to anint
. What you need is:However this only works if you only ever are going to have single digit integers (only 0 - 9). This might be better: