2

I have a string:

LogoDataStr = "ABC0000"

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

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?

Coolguy
  • 2,225
  • 12
  • 54
  • 81
  • 8
    Your code exactly returns what you want. Are you sure you're looking at the right array? – dtb Sep 19 '12 at 07:53
  • I agree - I tried the code and the results are correct (i.e. the array does not contains zeroes at the locations [3]..[6] but contains the correct ASCII codes). – Matthew Watson Sep 19 '12 at 07:56
  • dtb, aren't 0 suppose to be 0x30 in hex? – Coolguy Sep 19 '12 at 07:56
  • Matthew Watson, which result do you mean? – Coolguy Sep 19 '12 at 07:57
  • I've copied your code to Visual Studio and it returns the correct result: `'0'` becomes `0x30`. – dtb Sep 19 '12 at 07:58
  • @Coolguy to expand on what others are saying, if I try this (in .NET 4): `byte[] LogoDataBy=ASCIIEncoding.ASCII.GetBytes("ABC0000"); foreach(var b in LogoDataBy) Console.WriteLine("{0:x}",b);` I get 41 42 43 30 30 30 30 – Paolo Falabella Sep 19 '12 at 07:59
  • 1
    @Coolguy Not to resurect a dead post, but I'm curious what the problem was; I assume you fixed it, but I'm still interested. – Goku Mar 09 '20 at 15:23

3 Answers3

17

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.

oleksii
  • 35,458
  • 16
  • 93
  • 163
3

Just throwing:

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

Into LinqPAD gives an output of (decimal):

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

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

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
0
    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.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78