gpt4 book ai didi

C# 将字符串转换为 ASCII 字节

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

我有一个字符串:

LogoDataStr = "ABC0000"

我想转换成 ASCII 字节,结果应该是:

LogoDataBy[0] = 0x41;
LogoDataBy[1] = 0x42;
LogoDataBy[2] = 0x43;
LogoDataBy[3] = 0x30;
LogoDataBy[4] = 0x30;
LogoDataBy[5] = 0x30;
LogoDataBy[6] = 0x30;

我试过用这种方式:

byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes(LogoDataStr);

但是我得到的结果是这样的:

LogoDataBy[0] = 0x41;
LogoDataBy[1] = 0x42;
LogoDataBy[2] = 0x43;
LogoDataBy[3] = 0x00;
LogoDataBy[4] = 0x00;
LogoDataBy[5] = 0x00;
LogoDataBy[6] = 0x00;

我的编码有问题吗?

最佳答案

这段代码

class Program
{
static void Main(string[] args)
{
byte[] LogoDataBy = ASCIIEncoding.ASCII.GetBytes("ABC000");
}
}

产生预期的输出

enter image description here

在读取 ASCII 字节之前,请仔细检查您的代码和字符串的值。

关于C# 将字符串转换为 ASCII 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12490507/

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