简体   繁体   中英

How to convert byte string to byte array

I already have bytes needed stocked in a string but i don't know how to re convert it into byte.

    public static void sender(string message)
    {
        string path = "path of file here";
        byte[] pictureArray = File.ReadAllBytes(path);
        receiver("" + pictureArray);
    }

    public static void receiver(string message)
    {
        byte[] bytesReceived = message.ToByteArray(); //How i do this without convert my bytes into another bytes
    }

You can use the Encoding class, but you need to be sure which encoding you will be using.. (UTF8, Unicode etc)

public static void receiver(string message)
{
    byte[] bytesReceived = Encoding.Default.GetBytes(message);
}

and vice versa:

public static void sender(byte[] data)
{
    string stringReceived = Encoding.Default.GetString(data);
}

As I read, Encoding.Default return UTF-8.

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