gpt4 book ai didi

c# - 编写一个函数来自动将数字分类为 256 个单位的 block ?

转载 作者:行者123 更新时间:2023-11-30 23:19:21 24 4
gpt4 key购买 nike

我在 Unity 中有一个程序可以连接自定义 exe 文件并与之对话。我需要将 exe 文件值发送到可能在 12000 左右(尽管我可以发送的最大值约为 65500)。

数据需要分成两 block - |单位高达 256 | 256 的数量 |

一些例子:

如果我想发送 1000 的值,它将是“+ 232 + 3” (3 * 256 + 232)

如果我想发送 4878 的值,它将是“+ 14 + 19”(19 * 256 + 14)

我的问题是如何将其自动化为一个函数 - 绞尽脑汁也想不出办法。

我需要函数接收我需要发送给 exe 的值(即 1000),然后将其转换为包含两个整数值的字符串。对于这个 1000 示例,int1 因此将是 [232] 而 int2 将是 [3] ("+ int1 + int2") 或 ("+ 232 + 3")。

有人可以帮忙吗?

最佳答案

尝试使用模运算(/ 用于整数除法% 用于余数 ), 即

private static string Encode(int value) {
return $"+ {value % 256} + {value / 256}";
}

测试:

// + 232 + 3
Console.WriteLine(Encode(1000));
// + 14 + 19
Console.WriteLine(Encode(4878));

为了解码回来,您可以尝试使用Linq:

private static int Decode(string value) {
return value
.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries)
.Select(item => int.Parse(item))
.Aggregate((a, s) => s * 256 + a);
}

测试:

// 4878
Console.WriteLine(Decode("+ 14 + 19"));

关于c# - 编写一个函数来自动将数字分类为 256 个单位的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40342657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com