相关疑难解决方法(0)

在C#中将字符串转换为字节数组

我对C#很陌生.我正在将VB中的内容转换为C#.遇到此语句的语法问题:

if ((searchResult.Properties["user"].Count > 0))
{
    profile.User = System.Text.Encoding.UTF8.GetString(searchResult.Properties["user"][0]);
}
Run Code Online (Sandbox Code Playgroud)

然后我看到以下错误:

参数1:无法从'object'转换为'byte []'

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

我试图根据这篇文章修复代码,但仍然没有成功

string User = Encoding.UTF8.GetString("user", 0);
Run Code Online (Sandbox Code Playgroud)

有什么建议?

c# string encoding byte

594
推荐指数
14
解决办法
110万
查看次数

字符串值到字节数组而不转换

我试图将字符串的值放入字节数组而不更改字符.这是因为字符串实际上是数据的字节表示.

目标是将输入字符串移动到字节数组中,然后使用以下命令转换字节数组:

string result = System.Text.Encoding.UTF8.GetString(data);
Run Code Online (Sandbox Code Playgroud)

我希望有人可以帮助我,虽然我知道这不是一个很好的描述.

编辑:也许我应该解释一下,我正在研究的是一个带有文本框的简单窗体,用户可以将编码数据复制到其中,然后单击预览以查看解码数据.

编辑:多一点代码:(inputText是一个文本框)

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = inputString[i];
        }
        string output = base64Decode(input);
        this.inputText.Text = "";
        this.inputText.Text = output;
    }
Run Code Online (Sandbox Code Playgroud)

这是Windows窗体的一部分,它包含一个富文本框.这段代码不起作用,因为它不会让我将char类型转换为byte.但是,如果我将线路更改为:

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = (byte)inputString[i]; …
Run Code Online (Sandbox Code Playgroud)

.net c# casting

2
推荐指数
1
解决办法
2万
查看次数

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

我在阅读时出现错误:

无法将类型'String'隐式转换为'Byte []'

我认为'byte []'是字节数组-如果不是,请更正我。

我在此网站上尝试了另一种解决方案,但我不理解。我正在制作ac#“ RTM工具”,这是放在里面的内容:

byte[] bytes = (metroTextBox2.Text);   
Array.Resize<byte>(ref bytes, bytes.Length + 1);   
PS3.SetMemory(0x2708238, bytes);
Run Code Online (Sandbox Code Playgroud)

c# bytearray

-1
推荐指数
1
解决办法
7715
查看次数

标签 统计

c# ×3

.net ×1

byte ×1

bytearray ×1

casting ×1

encoding ×1

string ×1