Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将以字节为单位编码的字符串转换为以前的字符串值?_C#_Asp.net Core Webapi - Fatal编程技术网

C# 如何将以字节为单位编码的字符串转换为以前的字符串值?

C# 如何将以字节为单位编码的字符串转换为以前的字符串值?,c#,asp.net-core-webapi,C#,Asp.net Core Webapi,我在里面有一个文件,dGVzdA=,dGVzdA=是单词test在字节[]中转换 之后,我想让这个单词再次测试我的字符串,所以我这样做: [HttpPost("[Action]")] public async Task<IActionResult> ExtractLicenseData(IFormFile file){ string fileContents ; using (var stream = file.OpenReadStream()) {

我在里面有一个文件,dGVzdA=dGVzdA=是单词test在字节[]中转换

之后,我想让这个单词再次测试我的字符串,所以我这样做:

[HttpPost("[Action]")]
public async Task<IActionResult> ExtractLicenseData(IFormFile file){
    string fileContents ;

    using (var stream = file.OpenReadStream())
    {
        using (var reader = new StreamReader(stream))
        {
            fileContents = await reader.ReadToEndAsync();
        }
    }
    string test = Encoding.ASCII.GetString(fileContents); // error
    return BadRequest(test);
}
[HttpPost(“[Action]”)
公共异步任务提取LicenseData(IFormFile){
字符串文件内容;
使用(var stream=file.OpenReadStream())
{
使用(变量读取器=新的流读取器(流))
{
fileContents=wait reader.ReadToEndAsync();
}
}
字符串测试=Encoding.ASCII.GetString(fileContents);//错误
返回请求(测试);
}
但是当我尝试执行
string test=Encoding.ASCII.GetString(fileContents)时我有一个错误:

无法从“字符串”转换为“字节[]”

恢复:

我有我的字符串dGVzdA==我想最终将其转换为原始字符串test

/!\要从test转到dGVzdA==我要做的是
Encoding.ASCII.GetBytes(“test”)

dGVzdA==是字节[]中的字测试转换

否,
dGVzdA=
是字节0x74、0x65、0x73、0x74的base64编码表示形式。。。这又是文本“test”的ASCII编码表示形式

因此,如果您想要内容为0x74、0x65、0x73、0x74的字节数组,只需执行base64解码:

byte[] bytes = Convert.FromBase64String(fileContents);
然后,如果希望通过应用ASCII编码返回字符串,可以使用:

string text = Encoding.GetString(bytes);
但是,如果文件内容肯定是ASCII文本,那么最好首先避免使用base64部分。不清楚这是从哪里来的

要从
test
转到
dGVzdA==
我要这样做
Encoding.ASCII.GetBytes(“test”)


不,你没有
Encoding.ASCII.GetByte(“test”)
返回一个包含字节0x74、0x65、0x73、0x74的字节数组,而
dGVzdA=
是通过base64编码将该字节数组转换为字符串的结果。

因为它已经是
字符串
读取器.ReadToEndAsync()
将读取整个文件直到结束,并以
字符串的形式返回它

您不需要执行
编码.ASCII.GetString(fileContents),检查以下内容后的文件内容

using (var reader = new StreamReader(stream))
{
    fileContents = await reader.ReadToEndAsync();
    // here
}
现在做
Convert.FromBase64String(fileContents)在那里,您将拥有字节,而不是从字节中获取字符串