Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BinaryWriter错误地将两个字节额外写入C_C#_Hex_Binarywriter - Fatal编程技术网

C# BinaryWriter错误地将两个字节额外写入C

C# BinaryWriter错误地将两个字节额外写入C,c#,hex,binarywriter,C#,Hex,Binarywriter,首先,这是我写入指定偏移量的方法,我已经完成了调试过程,所有内容都得到了相应的设置 例如,我将pokemonValue设置为190,然后将其转换为0xBE,然后写入offsetArray[I]==0x1D104处的偏移量。预期的行为是,它将仅在该偏移量处写入0xBE。但事实并非如此。相反,它分别在0x1D104 0x1D105 0x1D106处写入0x02 0x42 0x45 public void writeStarterPokemon(long[] offsetArray,

首先,这是我写入指定偏移量的方法,我已经完成了调试过程,所有内容都得到了相应的设置

例如,我将pokemonValue设置为190,然后将其转换为0xBE,然后写入offsetArray[I]==0x1D104处的偏移量。预期的行为是,它将仅在该偏移量处写入0xBE。但事实并非如此。相反,它分别在0x1D104 0x1D105 0x1D106处写入0x02 0x42 0x45

        public void writeStarterPokemon(long[] offsetArray, BinaryWriter writer, int pokemonValue)
        {
            string hexVal = "";
            for (int i = 0; i < offsetArray.Length; i++)
            {
                writer.BaseStream.Position = offsetArray[i];
                hexVal = string.Format("{0:X}", pokemonValue); // pokemonValue is a decimal ranging from 0-255;
                MessageBox.Show(string.Format("Hex val: 0x{0:1X}, Offset: 0x{1:X5}", hexVal, offsetArray[i])); // to see if the values are correct
                writer.Write(hexVal);
                writer.Flush();
            }
        }

我已经检查了我的偏移量,它们是正确的,在程序的早期,我从它们那里读到了它的预期效果。因此,我不确定这为什么不能正常工作,或者更确切地说,将数据设置为不正确。

如果要写入特定字节,则需要使用byte[]等API。如果您使用Writestring API,它会为字符串长度等编码额外的数据,这可能是您不想要的。坦白地说,BinaryWriter在大多数情况下都不是很有用-最好是写入流等,这将限制您使用能够实现预期目的的API。

如果您想要写入特定字节,则需要使用byte[]等API。如果您使用Writestring API,它会为字符串长度等编码额外的数据,这可能是您不想要的。坦白地说,BinaryWriter在大多数情况下都不是很有用-最好是写入流等,这将限制您使用能够实现预期功能的API。

BinaryWriter在字符串前面加上长度前缀,这就是0x02的来源。看起来您想要的是实际写入二进制值,而不是字符串。pokemonValue是否总是在0到255之间?@MatthewWatson啊,好的,这很有意义。是的,它始终在该范围内。在这种情况下,您可以尝试writer.WritebytepokemonValue;-或者使用writer.BaseStream.WriteBytePokeMonValue@MatthewWatson writer.BaseStream.WriteBytePokeMonValue;我用过这个,它很管用,谢谢@David像这样混合和匹配API通常不是一个好主意-在很多情况下,你会被缓冲BinaryWriter前缀字符串的长度所困扰,这就是0x02的来源。看起来您想要的是实际写入二进制值,而不是字符串。pokemonValue是否总是在0到255之间?@MatthewWatson啊,好的,这很有意义。是的,它始终在该范围内。在这种情况下,您可以尝试writer.WritebytepokemonValue;-或者使用writer.BaseStream.WriteBytePokeMonValue@MatthewWatson writer.BaseStream.WriteBytePokeMonValue;我用过这个,它很管用,谢谢@David像这样混合和匹配API通常不是一个好主意-在很多情况下,缓冲会让你感到痛苦
private long[] squirtleOffsets = new long[] { 0x1D104, 0x1D11F, 0x24BA5, 0x26FBC};

writeStarterPokemon(sqrtlOffsets, writer, NameList.SelectedIndex); 
// NameList is the name of my comboBox populated with pokemon data, 0-255