【问题标题】:The value for an unsigned byte was too large or too small无符号字节的值太大或太小
【发布时间】:2016-12-07 17:43:49
【问题描述】:

我正在尝试向游戏写入一些字节。

函数源码:

public void updateStatistic(string prestigeValue, string experience, string winrate, string loserate)
{
    VAMemory vam = new VAMemory(process);
    vam.WriteByte((IntPtr)0x145D114B9, byte.Parse(prestigeValue));
    vam.WriteByte((IntPtr)0x145D114B5, byte.Parse(experience));
    vam.WriteByte((IntPtr)0x145D10E05, byte.Parse(winrate));
    vam.WriteByte((IntPtr)0x145D12240, byte.Parse(loserate));
}

很遗憾,我收到以下错误消息:

An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll

Additional information: The value for an unsigned byte was too large or too small.

地址的类型是 4 Bytes。我要发布的值是:prestigeValue = 1,experience = 1500000,winrate = 100,losrate=50

有谁知道我怎样才能让它工作?

【问题讨论】:

  • 异常来自byte.Parse(),因此prestigeValueexperiencewinrateloserate中的一个(或多个)的值小于0或大于255如here所示。您可能对每个字节位置实际存储了哪些值存在误解。
  • 数值如下:prestigeValue = 1, experience = 1500000, winrate = 100, lostrate= 50
  • 一个字节不能容纳 150,000 的值。是什么让您认为 experience 值是从该 1 字节内存位置读取的?如果您认为该值确实保存在该字节中是正确的,那么我的第一个猜测是他们取该值并在显示之前将其乘以 10,000,例如,该值实际上可能是 150 作为一个字节,但在显示时显示为 150,000 .

标签: c# memory byte


【解决方案1】:

好的,首先让我们从头开始。

VAMemory 可能是从这个参考中获得的 DLL。 http://www.vivid-abstractions.net/logical/programming/vamemory-c-memory-class-net-3-5/

它允许您写入特定的内存位置。对于做某些事情非常有用。

由于您尝试写入的值是 32 位值。你为什么不使用

vam.WriteInt32((IntPtr)0x145D114B5, byte.Parse(experience));

相反?

或者,您可以将字节分解为单个字节,例如

var experience_int = int.parse(experience);
vam.WriteByte((IntPtr)0x145D114B5, (byte)experience_int & 0xFF);
vam.WriteByte((IntPtr)0x145D114B6, (byte)(experience_int>>8) & 0xFF);
vam.WriteByte((IntPtr)0x145D114B7, (byte)(experience_int>>16 & 0xFF);
vam.WriteByte((IntPtr)0x145D114B8, (byte)(experience_int>>24) & 0xFF);

请注意,我没有测试代码,也没有检查顺序,但它应该是这样的。

更多信息: 你得到提到的异常的原因是因为当你调用 Byte.Parse() 但你输入的字符串大于 255。你可以看到 mscorlib.dll 是抛出异常的地方。在 MSCoreLib 内部,异常是这样抛出的。

if (num < 0 || num > (int) byte.MaxValue) 
    throw new OverflowException(Environment.GetResourceString("Overflow_Byte"));

【讨论】:

    【解决方案2】:

    尝试使用 int.Parse(value)

    1500000 太大了,无法存储在一个字节中。

    编辑:您还需要找到一种将 int 写入内存的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多