Josh
Josh

Reputation: 1019

How to convert binary string to bytes[] array?

Since Mozilla's btoa and atob aren't compatible with IE, Im using Nick Galbreath's solution that works across the board.

In my JS, I have this snippet:

reader.onload = function (e)
{
    var base64str = e.target.result.split(';')[1].split(',')[1];
    var binaryData = base64.decode(base64str); 
    
    // binaryData looks like: 3!1AQa"q2¡±B#$RÁb34rÑC%Sðáñcs5¢²&DTdE£t
    // 6ÒUâeò³ÃÓuãóF'¤´ÄÔäô¥µÅÕåõVfv¦¶ÆÖæö7GWgw§·Ç×ç÷5!1AQaq"2¡±B#ÁRÑð
    // 3$bárCScs4ñ%¢²&5ÂÒDT£dEU6teâò³ÃÓuãóF¤´ÄÔäô¥µÅÕåõVfv¦¶ÆÖæö'7GWgw
    // §·ÇÿÚ?õTI%)$IJI$RIrÿ[múÙxÝ^«ÝKØrþk²ïÑûíGóß÷¿ÑþÄY«ÍÓ±×úN //...
    // Is this even binary data?

    Ajax.SendToHandler(binaryData);
}

How do I convert binaryData, which is sent to my ashx derived IHttpHandler as a string, into a bytes[] array?

Ask me to clarify where needed!

Upvotes: 0

Views: 3156

Answers (1)

Medinoc
Medinoc

Reputation: 6608

Your data string seems to contain only extended ASCII characters (probably either Windows-1252 characters or ISO 8859-1 characters). You should try using a System.Text.Encoding to convert it to bytes.

Upvotes: 2

Related Questions