繁体   English   中英

如何将字节字符串转换为字节数组

[英]How to convert byte string to byte array

我已经在字符串中存储了所需的字节,但我不知道如何将其重新转换为字节。

    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
    }

您可以使用Encoding类,但您需要确定您将使用哪种编码。 (UTF8、Unicode 等)

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

反之亦然:

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

正如我所读到的, Encoding.Default返回 UTF-8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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