Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print hex or decimal values of byte array in JavaScript

I have a function in JavaScript which is expecting a byte array to be passed into it from Native C++ code. For example:

function OnEvent(event, data1)
{
     console.log("data1[0] = " + data1[0]);
}

I would like for it to print 0x55 or even the decimal value of it. For some reason, I am seeing the following in the console log:

enter image description here

How can I print the hex value of the byte or even the decimal value without printing the character?


UPDATE

I went to the link below thanks to Vinothbabu. I used the unpack function:

function unpack(str) { 
    var bytes = []; 
    $("#homePage").append("str.length = " + str.length + "<br>"); 

    for(var i = 0, n = str.length; i < n; i++) { 
        var char = str.charCodeAt(i); 
        $("#homePage").append("char is equal to " + char + "<br>"); 
        bytes.push(char >>> 8, char & 0xFF); 
    }

    return bytes; 
}

It prints "char is equal to 65533" and the value of "bytes" prints out "255, 253" which means it has a value of 0xFFFD.

This is not the data/payload that I was expecting. Do you know why there are 2 bytes?

like image 583
code Avatar asked Dec 28 '25 07:12

code


1 Answers

I believe this will do the trick:

console.log("data1[0] = " + data1[0].toString(16));
like image 74
William Gaul Avatar answered Dec 30 '25 20:12

William Gaul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!