I'm trying to transform a bytes array into a string in javascript. But first, i must cast an object to the byte array.
Here is a sample:
function Main(obj)
{
//Obj is an object (in fact, it's a bytes array
var str = FromBytesToString(obj);
//str must be a string, computed from the obj
return str;
}
Anyone has an idea of how to do that ?
Thanks in advance,
Guillaume
EDIT: some precisions:
1) I call the javascript in a windows application (C#), with this piece of code:
private string ExecuteScript(byte[] buffer)
{
//Load script (using StreamReader)
string script = LoadScript(@"C:\script.js");
//Parse script
ScriptEngine engine = new ScriptEngine("Jscript");
ParsedScript parsedScript = engine.Parse(script);
//Run script, calling "Main" method
return parsedScript.CallMethod("Main", buffer);
}
This code use ScriptEngine code, found here. It uses Windows Script Engine
2) Javascript code
Here is the javascript code:
function Main(bytearray)
{
//Transform the bytearray in string
str = StringFromBytes();
//Do some stuff (replace/etc)
//sent back the new string
return str;
}
The problem is thus that the argument "bytearray" is a C# byte array, and javascript knows it only as object. If i use the method below:
function StringFromByte(array)
{
var b = array;
var s = "";
for (var i = 0; i < b.length; i++)
s += String.fromCharCode(b[i]);
return s;
}
An error in shown at line "s+=String.fromCharCode(b[i]); -> A number is expected...