将字符串转换为字节数组的正确编码是什么

时间:2022-03-19 18:15:45

I am having some sort of problem with encoding in my ASP.NET HTTPHandler, which uploads a file. The file content is passed in a hidden form variable from a ColdFusion web page which is using something called "ToBase64".

我在ASP中遇到了一些编码问题。NET HTTPHandler,它上传一个文件。文件内容在ColdFusion web页面的隐藏表单变量中传递,该页面使用的是“ToBase64”。

In ColdFusion, the code used to place the file content into a form is as follows:

在ColdFusion中,用于将文件内容放入表单的代码如下:

<cffile action="readBinary" file="#FileName#" variable="objBinaryData">
    <cfset b64file = #toBase64(objBinaryData)#>
<form name="sendToHandler" 
           action="http://myserver/mysite/UploadHandler.ashx" method="post">
   <cfoutput>
       <input type="hidden" name="objBinaryData" value="#b64file#" />

When my UploadHandler.ashx is posted, I am getting a string out of the form as follows:

当我UploadHandler。ashx已经发布,我正在从表单中获取如下的字符串:

            string fileContent = context.Request.Form["objBinaryData"];

Next, I am converting the string to a byte array as follows:

接下来,我将字符串转换为字节数组,如下所示:

            byte[] binData = StringToByteArray(fileContent, EncodingType.ASCII);

Here is the function I'm using to convert the string:

这是我用来转换字符串的函数:

        public static byte[] StringToByteArray(string str, EncodingType encodingType)
    {
        System.Text.Encoding encoding = null;
        switch (encodingType)
        {
            case EncodingType.ASCII:
                encoding = new System.Text.ASCIIEncoding();
                break;
            case EncodingType.Unicode:
                encoding = new System.Text.UnicodeEncoding();
                break;
            case EncodingType.UTF7:
                encoding = new System.Text.UTF7Encoding();
                break;
            case EncodingType.UTF8:
                encoding = new System.Text.UTF8Encoding();
                break;
        }
        return encoding.GetBytes(str);
    }
public enum EncodingType
    {
        ASCII,
        Unicode,
        UTF7,
        UTF8
    }

It's obvious to me that calling the above function with EncodingType.ASCII is wrong but I am very confused about what would be correct? What is the proper "match" between "Base64" sent from ColdFusion and the way the string should be encoded in .Net?

很明显,用EncodingType调用上面的函数。ASCII码是错误的,但是我很困惑什么是正确的?ColdFusion发送的“Base64”与.Net中字符串的编码方式之间的“匹配”是什么?

Please note that all the code "works" but the subsequent retrieval of a file shows it to be scrambled and I'm pretty sure I have the wrong encoding here.

请注意,所有的代码都是“工作”的,但是随后对文件的检索显示它被打乱了,我很确定我在这里有错误的编码。

EDIT-update:

EDIT-update:

I added the enum code previously omitted. I've tried all of these Encoding Types; they all result in "garbage". That is: I have tried each of these variations:

我添加了前面省略的enum代码。我尝试过所有这些编码类型;它们都会导致“垃圾”。也就是说:我尝试过每一种变化:

byte[] binData = StringToByteArray(fileContent, EncodingType.ASCII);
byte[] binData = StringToByteArray(fileContent, EncodingType.Unicode);
byte[] binData = StringToByteArray(fileContent, EncodingType.UTF7);
byte[] binData = StringToByteArray(fileContent, EncodingType.UTF8);

None of these work properly. As I read your suggested function, it should be Unicode. Note that I want to return a byte array not a converted string. Still very confused.

这些都不能正常工作。当我读到您建议的函数时,它应该是Unicode。注意,我想返回一个字节数组,而不是一个转换后的字符串。仍然很困惑。

ANSWER:

答:

I simply eliminated the enum and the function I wrote called StringToByteArray. Instead I coded the following:

我只是删除了enum和我编写的名为StringToByteArray的函数。相反,我编写了以下代码:

byte[] binData = Convert.FromBase64String(fileContent); 

2 个解决方案

#1


3  

Look at the Convert.FromBase64String() function

查看Convert.FromBase64String()函数

#2


2  

Base64 is an encoding scheme that enables you to represent binary data as a series of ASCII characters so that it can be included in text files and e-mail messages in which raw binary data is unacceptable. The below examples show encoding and decoding of unicode strings. Let me know if this is what you wanted,if not I can refind this further for you.

Base64是一种编码方案,它使您能够将二进制数据表示为一系列ASCII字符,以便将其包含在文本文件和电子邮件中,其中原始二进制数据是不可接受的。下面的示例显示unicode字符串的编码和解码。如果这是你想要的,请告诉我,如果不是,我可以再为你找到这个。

//Encoding
 public static string StringToBase64 (string src) {

    // Get's byte representation unicode string
    byte[] b = Encoding.Unicode.GetBytes(src);

    // Returns Base64-encoded string
    return Convert.ToBase64String(b);

}
//Decoding
public static string Base64ToString (string src) {

    // Decodes Base64-encoded string to a byte array
    byte[] b = Convert.FromBase64String(src);

    // Returns decoded Unicode string
    return Encoding.Unicode.GetString(b);
}

#1


3  

Look at the Convert.FromBase64String() function

查看Convert.FromBase64String()函数

#2


2  

Base64 is an encoding scheme that enables you to represent binary data as a series of ASCII characters so that it can be included in text files and e-mail messages in which raw binary data is unacceptable. The below examples show encoding and decoding of unicode strings. Let me know if this is what you wanted,if not I can refind this further for you.

Base64是一种编码方案,它使您能够将二进制数据表示为一系列ASCII字符,以便将其包含在文本文件和电子邮件中,其中原始二进制数据是不可接受的。下面的示例显示unicode字符串的编码和解码。如果这是你想要的,请告诉我,如果不是,我可以再为你找到这个。

//Encoding
 public static string StringToBase64 (string src) {

    // Get's byte representation unicode string
    byte[] b = Encoding.Unicode.GetBytes(src);

    // Returns Base64-encoded string
    return Convert.ToBase64String(b);

}
//Decoding
public static string Base64ToString (string src) {

    // Decodes Base64-encoded string to a byte array
    byte[] b = Convert.FromBase64String(src);

    // Returns decoded Unicode string
    return Encoding.Unicode.GetString(b);
}