在C#转换字符串的字节数组 [英] Converting string to byte array in C#

查看:88
本文介绍了在C#转换字符串的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#。我转换的东西从VB到C#。有这个语句的语法问题:

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:

参数1:不能从对象转换为字节[]

则是'System.Text.Encoding.GetString(字节[])'最佳重载的方法匹配具有一些无效参数

我试图根据<一个固定code href=\"http://stackoverflow.com/questions/10180864/converting-byte-array-to-string-not-working-in-c-sharp\">this职位,但仍然没有成功。

I tried to fix the code based on this post, but still no success

string User = Encoding.UTF8.GetString("user", 0);

有什么建议?

推荐答案

如果你已经有了一个字节数组,那么你需要知道使用的是什么类型的编码,使之成为该字节数组。

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);

如果你能在你继承了code,用于创建字节数组,那么你应该设置编码找到。

If you can find in the code you inherited, the encoding used to create the byte array then you should be set.

这篇关于在C#转换字符串的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆