Last active
December 9, 2015 15:15
-
-
Save vibhavsinha/11344013 to your computer and use it in GitHub Desktop.
Raw binary Communication between actionscript and javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected function decode(str:String):ByteArray | |
{ | |
var alphabet:String = 'abcdefghijklmnopqrstuvwxyz'; // only first 16 chars used | |
var lookup:Object = new Object; | |
var len:Number = str.length; | |
var enc:Array = [0,0]; | |
var result:ByteArray = new ByteArray; | |
if(str.length % 2) | |
throw new Error("Decode failed: The string is not correctly encoded."); | |
var position:Number = -1; | |
while(++position < 26) | |
lookup[alphabet.charAt(position)] = position; | |
position = 0; | |
while(position < len){ | |
enc[0] = lookup[str.charAt(position)]; | |
enc[1] = lookup[str.charAt(position+1)]; | |
position+=2; | |
result.writeByte(-128 + 16*enc[0] + enc[1]); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//encode the buffer from position of length len | |
function encode (buffer, position, len){ | |
var len = Math.min(len, buffer.byteLength-(position)); | |
position = position-1; | |
var dataView = new DataView(buffer); | |
var result = ""; | |
var n = 0; | |
len = len + position + 1; | |
while(++position < len){ | |
n = 128 + dataView.getUint8(position); | |
result += B64.alphabet.charAt(n / 16) + B64.alphabet.charAt(n % 16) ; | |
} | |
//console.log(result.length); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment