简体   繁体   中英

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.

You get bytes based on the encoding.

eg

Encoding.UTF8.GetBytes(string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM