python - How do I convert Bytes to Integers in Javascript? - Stack Overflow

I'm running a server on Python which sends data to JavaScript. This server however can only send b

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'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! :)

Share Improve this question asked Jun 18, 2020 at 3:03 divad99divad99 231 gold badge1 silver badge5 bronze badges 2
  • Just to be clear in Javascript you are getting a string line "b'120'"? That doesn't seem correct as that's how python represents bytes as a string. – Mark Commented Jun 18, 2020 at 3:16
  • 1 Yes! You are correct, I was pasing it through the "str()" method! @MarkMeyer – divad99 Commented Jun 18, 2020 at 4:26
Add a ment  | 

2 Answers 2

Reset to default 3

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;
};

I hope this helps

function binToInt(bin){
	return parseInt(bin, 2);
}

console.log(binToInt("00101001"));	//Outputs 41

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742311608a4420004.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信