1

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

Community
  • 1
  • 1
Guillaume
  • 11
  • 1
  • 4

2 Answers2

0

How about proper call to StringFromByte in Main function passing hte bytearray to it ?

function Main(bytearray)
{
//Transform the bytearray in string
str = StringFromBytes(bytearray);   //<<<< this was missing
//Do some stuff (replace/etc)

//sent back the new string
return str;    
}
0

You can;

var b = [0x61, 0x62, 0x63];
var s = "";
for (var i = 0; i < b.length; i++)
    s += String.fromCharCode(b[i]);

s === "abc"
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • It works with your sample, but not with my object. It says that a Number is expected at line "s += String.fromCharCode(b[i]). I think the problem comes from the fact that this object is a byte[] in C#, passed as argument, from Windows Script Engine – Guillaume Mar 13 '12 at 11:59
  • I assumed that you had an array of bytes already, what does the object look like? – Alex K. Mar 13 '12 at 12:00
  • I assume that it's an object, i don't know any javascript methods to get the Type of the object :s – Guillaume Mar 13 '12 at 12:03
  • Guillaume, when using chrome/safari, you can use the console to inspect what an object consists of. AFAIK that works for FireBug as well. – Dykam Mar 13 '12 at 12:08
  • console.dir(theobject) will print its structure in the f12 debug console – Alex K. Mar 13 '12 at 12:09
  • The problem is that my javascript code is executed in an windows application, not in a browser :). Ps: in C# code, the object is a byte[] – Guillaume Mar 13 '12 at 12:13
  • How are you adding the .net array? script controls .run() ? – Alex K. Mar 13 '12 at 12:30
  • (For information, a typeof() (in javascript code) on the byte array passed from C# return an "unknown" value... – Guillaume Mar 13 '12 at 14:44