从文件中读取 int 返回 ASCII

发布于 2024-12-25 02:35:45 字数 1315 浏览 1 评论 0原文

我目前正在制作一个游戏,但我似乎在从文本文件读取值时遇到问题。由于某种原因,当我读取该值时,它会给出该值的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你怎么这么可爱啊 2025-01-01 02:35:45

这就是转换为 ascii 的地方:

fileWrite.Write(level.GetValueAtIndex(i, j) + " ");

+ 运算符隐式地将 GetValueAtIndex 返回的整数转换为字符串,因为您要将其添加到字符串中(真的,您期望发生什么?)

此外,ReadLine 方法返回一个字符串,所以我不确定为什么你会期望一个数值神奇地回到这里。如果您想写入二进制数据,请查看 BinaryWriter

This is where the convertion to ascii is happening:

fileWrite.Write(level.GetValueAtIndex(i, j) + " ");

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

踏月而来 2025-01-01 02:35:45

您可以在此处将字符转换为字符代码:

num = (int)id;

id 变量是 char,将其转换为 int 即可得到字符代码,而不是数值。

此外,这会转换单个字符,而不是整数。例如,如果您的文本文件中有 "12 34 56 ",它将获得 1、2、3、4、5 和 6 的代码,而不是 12、34 和 56。

您会想要以空格分割行,并解析每个子字符串:

foreach (string id in str.Split(' ')) {
  if (id.Length > 0) {
    num = Int32.Parse(id);
    level.ChangeTile(i, iter, num);
    iter++;
  }
}

This is where you are converting the characters to character codes:

num = (int)id;

The id variable is a char, and casting that to int 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:

foreach (string id in str.Split(' ')) {
  if (id.Length > 0) {
    num = Int32.Parse(id);
    level.ChangeTile(i, iter, num);
    iter++;
  }
}
情栀口红 2025-01-01 02:35:45

更新:我保留了旧代码(如下),并假设每行都有一个记录,但我还添加了一种不同的方法,该方法应该可以处理一个记录上的多个整数行,用空格分隔。

一行上有多个记录

str = fileRead.ReadLine();

string[] values = str.Split(new Char[] {' '});

foreach (string value in values)
{
    int testNum;

    if (Int32.TryParse(str, out testnum))
    {
        // again, not sure how you're using iter here
        level.ChangeTile(i, iter, num);
    }
}

每行一个记录

str = fileRead.ReadLine();

int testNum;

if (Int32.TryParse(str, out testnum))
{
    // however, I'm not sure how you're using iter here; if it's related to 
    // parsing the string, you'll probably need to do something else
    level.ChangeTile(i, iter, num);
}

请注意,如果您逐行写出每个整数(即您如何通过您在上面的代码中注释掉了 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

str = fileRead.ReadLine();

string[] values = str.Split(new Char[] {' '});

foreach (string value in values)
{
    int testNum;

    if (Int32.TryParse(str, out testnum))
    {
        // again, not sure how you're using iter here
        level.ChangeTile(i, iter, num);
    }
}

One record per line

str = fileRead.ReadLine();

int testNum;

if (Int32.TryParse(str, out testnum))
{
    // however, I'm not sure how you're using iter here; if it's related to 
    // parsing the string, you'll probably need to do something else
    level.ChangeTile(i, iter, num);
}

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.

み青杉依旧 2025-01-01 02:35:45

你有:

foreach (char id in str)
{
    //convert id to an int
    num = (int)id;

char 一个 ASCII 代码(或者可以被认为是这样的;从技术上讲,它是一个 unicode 代码点,但假设你正在编写 ANSI 代码,那么它是大致可比的或低值 UTF-8)。

你想要的是:

num = (int)(id - '0');

You have:

foreach (char id in str)
{
    //convert id to an int
    num = (int)id;

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:

num = (int)(id - '0');
慕烟庭风 2025-01-01 02:35:45

此操作:

fileWrite.Write(level.GetValueAtIndex(i, j) + " ");

将从 level.GetValueAtIndex(i, j) 返回的 int 转换为字符串。假设该函数对于特定的 ij 返回值 5,那么您将“5”写入文件中。

然后,当您读取时,它会被读取为由字符组成的字符串,并且当您将其简单地转换为 int 时,您会得到 5 的 ASCII 代码。你需要的是:

foreach (char id in str)
{
    if (id != ' ')
    {
        //convert id to an int
        num = (int)(id - '0'); // subtract the ASCII value for 0 from your current id
        level.ChangeTile(i, iter, num);
        iter++;
    }
}

然而,只有当你只有个位数整数(只有 0 - 9)时,这才有效。这可能会更好:

foreach (var cell in fileRead.ReadLine().Split(' '))
{
     num = Int.Parse(cell);
     level.ChangeTile(i, iter, num);
     iter++;
}

This:

fileWrite.Write(level.GetValueAtIndex(i, j) + " ");

converts the int returned from level.GetValueAtIndex(i, j) into a string. Assuming the function returns the value 5 for a particular i and j 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 an int. What you need is:

foreach (char id in str)
{
    if (id != ' ')
    {
        //convert id to an int
        num = (int)(id - '0'); // subtract the ASCII value for 0 from your current id
        level.ChangeTile(i, iter, num);
        iter++;
    }
}

However this only works if you only ever are going to have single digit integers (only 0 - 9). This might be better:

foreach (var cell in fileRead.ReadLine().Split(' '))
{
     num = Int.Parse(cell);
     level.ChangeTile(i, iter, num);
     iter++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文