I'm running a server on Python which sends data to JavaScript. This server however can only send bytes. I'm needing the data to be in integer form. Is there a way to convert this in Javascript.
Here's the line of Code that's receiving the data. How can I transform evt.data from bytes to Integers. What I'm receiving from Python is the b' followed by the number. Example: b'120'
ws.onmessage = function (evt) {
var received_msg = evt.data;
Here is the line of code that is used to Send data from Python. It's UDP and unfortunately can only send bytes. I'm sending data from a python server to python client and then to a python websocket server. That python web socket server unfortunately can't send over the bytes converted to ints via the " int() " method.
sock.sendto(bytes(MESSAGE, "utf-8"), (ip_address, UDP_PORT))
What do I need to do? Thanks in Advance: :)
I hope this helps
function binToInt(bin){ return parseInt(bin, 2); } console.log(binToInt("00101001")); //Outputs 41
Javascript doesn't support 64 bits integers. That said this function should do the trick for 32 bit signed integers:
var byteArrayToInt = function(byteArray) {
var value = 0;
for (var i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + byteArray[i];
}
return value;
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.