简体   繁体   English

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

[英]How to convert a byte array to a string?

Using the function from: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx 使用以下函数: http//msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

public static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)

As you can see it returns a byte array, I want to convert the byte array to a string. 如您所见,它返回一个字节数组,我想将字节数组转换为字符串。

How can I convert it from a byte array to string and visa versa? 如何将它从字节数组转换为字符串,反之亦然?

If you don't care how it's stored, an easy way is to use: 如果你不关心它是如何存储的,一个简单的方法是使用:

Convert byte array into string: Convert.ToBase64String(YourByteArray) and 将字节数组转换为字符串: Convert.ToBase64String(YourByteArray)
Convert string into byte array: Convert.FromBase64String(YourString) . 将字符串转换为字节数组: Convert.FromBase64String(YourString)
This will give a concise, printable ASCII representation of the byte array. 这将给出字节数组的简洁,可打印的ASCII表示。

这可以帮助你很多,即将转换为十六进制格式,但可能非常有用如何将字节数组转换为十六进制字符串,反之亦然?

While using Rijndael Encryption i faced this problem, it returns encrypted byte[] (array), Convert byte[] to string; 在使用Rijndael Encryption时遇到了这个问题,它返回加密的byte [](数组),将byte []转换为字符串;

 myStringVariable= Convert.ToBase64String(myEncryptedByteArray);  

Convert string to byte[]; 将字符串转换为byte [];

byte[] bytes = Convert.FromBase64String(myStringVariable);   

For more about Rijndael 有关Rijndael的更多信息

Cheers !!! 干杯!!!

System.Text.Encoding.ASCII.GetString(bytes);

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

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