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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12490507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:25:38  来源:igfitidea点击:

C# Convert a string to ASCII bytes

c#

提问by Coolguy

I have a string:

我有一个字符串:

LogoDataStr = "ABC0000"

I want to convert to ASCII bytes and the result should be:

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

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

I've tried using this way:

我试过用这种方式:

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

But the result I get is this:

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

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

Is there any wrong with my coding?

我的编码有问题吗?

回答by oleksii

This code

这段代码

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

produces expected output

产生预期输出

enter image description here

在此处输入图片说明

Double check your code and the value of the string before you read ASCII bytes.

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

回答by Rowland Shaw

Just throwing:

只是扔:

Encoding.ASCII.GetBytes("ABC0000").Dump();

Into LinqPAD gives an output of (decimal):

进入 LinqPAD 给出(十进制)的输出:

Byte[] (7 items)
65
66
67
48
48
48
48

字节[] (7 项)
65
66
67
48
48
48
48

So I'm not sure how you're getting 0x00...

所以我不确定你是如何得到 0x00 的...

回答by Aniket Inge

    class CustomAscii
    {
        private static Dictionary<char, byte> dictionary;

        static CustomAscii()
        {
            byte numcounter = 0x30;
            byte charcounter = 0x41;
            byte ucharcounter = 0x61;
            string numbers = "0123456789";
            string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string uchars = "abcdefghijklmnopqrstuvwxyz";
            dictionary = new Dictionary<char, byte>();
            foreach (char c in numbers)
            {
                dictionary.Add(c, numcounter++);
            }
            foreach (char c in chars)
            {
                dictionary.Add(c, charcounter++);
            }
            foreach (char c in uchars)
            {
                dictionary.Add(c, ucharcounter++);
            }
        }

        public static byte[] getCustomBytes(string t)
        {
            int iter = 0;
            byte[] b = new byte[t.Length];
            foreach (char c in t)
            {
                b[iter] = dictionary[c];
                //DEBUG: Console.WriteLine(b[iter++].ToString());
            }

            return b;
        }
    }

This is how i would do it. JUST IF Encoding.ASCII.GetBytes() would return wrong values.

这就是我要做的。只是如果 Encoding.ASCII.GetBytes() 会返回错误的值。