R – proper encoding for converting a string to a byte array

asp.netbytearraycoldfusionencodingstring

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".

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

<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:

            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?

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:

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:

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.

ANSWER:

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

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

Best Answer

Look at the Convert.FromBase64String() function