将字符串转换为C#中的字节数组 [英] Converting string to byte array in C#
问题描述
if((searchResult.Properties [user]。Count> 0 ))
{
profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties [user] [0]);
}
然后我看到以下错误:
参数1:无法从对象转换为byte []
最佳重载方法匹配
'System.Text.Encoding.GetString(byte [])'有一些无效的参数
我试图修复代码基于此帖子,但仍然没有成功
string User = Encoding.UTF8.GetString(user,0);
任何建议?
如果您已经有一个字节数组,那么您将需要知道使用哪种类型的编码来使其成为该字节数组。
例如,如果字节数组是这样创建的:
byte [] toBytes = Encoding.ASCII.GetBytes(somestring);
您将需要将其重新转换成如下所示的字符串:
string something = Encoding.ASCII.GetString(toBytes);
如果您可以在继承的代码中找到用于创建字节数组的编码,那么应该设置。
I'm quite new to C#. I'm converting something from VB into C#. Having a problem with the syntax of this statement:
if ((searchResult.Properties["user"].Count > 0))
{
profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]);
}
I then see the following errors:
Argument 1: cannot convert from 'object' to 'byte[]'
The best overloaded method match for 'System.Text.Encoding.GetString(byte[])' has some invalid arguments
I tried to fix the code based on this post, but still no success
string User = Encoding.UTF8.GetString("user", 0);
Any suggestions?
If you already have a byte array then you will need to know what type of encoding was used to make it into that byte array.
For example, if the byte array was created like this:
byte[] toBytes = Encoding.ASCII.GetBytes(somestring);
You will need to turn it back into a string like this:
string something = Encoding.ASCII.GetString(toBytes);
If you can find in the code you inherited, the encoding used to create the byte array then you should be set.
这篇关于将字符串转换为C#中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!