Andrei Cristian Prodan
Andrei Cristian Prodan

Reputation: 1122

How do I convert a string into a byte array in C#?

I have a piece of text and I want to turn it into a byte array like this one:

        byte[,] input = new byte[4, 4] {
                                        { 0xd4, 0xe0, 0xb8, 0x1e },
                                        { 0xbf, 0xb4, 0x41, 0x27 },
                                        { 0x5d, 0x52, 0x11, 0x98 },
                                        { 0x30, 0xae, 0xf1, 0xe5 } 
                                       };

I'm giving the example because I have a method that can only take arguments as byte array (input is a valid argument) and I want to make sure I'm asking the right question.

Upvotes: 0

Views: 219

Answers (1)

Joe
Joe

Reputation: 42656

You get bytes based on the encoding.

e.g.

Encoding.UTF8.GetBytes(string)

Upvotes: 6

Related Questions