Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert a string to ASCII bytes

Tags:

c#

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?

like image 383
Coolguy Avatar asked Mar 09 '26 14:03

Coolguy


1 Answers

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.

like image 173
oleksii Avatar answered Mar 11 '26 02:03

oleksii