1

I am trying to convert a byte array into a string and then send it over a socket to a remote server. I have successfully prototyped the code in Python and am trying to migrate it to Javascript.

For some reason, there is a discrepancy in the last character between the two languages.

Python Code

def make_checksum(data):
    num = 0x00
    for num2 in data:
        num = (num + num2) & 0xFF
    return num

data = [0x56, 0x54, 0x55, 0x3E, 0x28, 0x00, 0x08, 
        0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0]
message = bytearray(data + [make_checksum(data)])

Javascript

function checksum(data) {
    let res = 0x00
    for (let i = 0; i < data.length; ++i) {
        res = (res + data[i]) & 0xFF
    }
    return String.fromCharCode(res)
}

let data = new Int8Array([0x56, 0x54, 0x55, 0x3E, 0x28, 0x00,
           0x08, 0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0])
let message = String.fromCharCode(...data) + checksum(data)

I think this might have something to do with the difference between ascii and UTF.

12
  • 3
    Should the javascript be doing checksum(data) instead of checksum(0xC0)? Should the python be doing bytearray(data + [make_checksum(data)])? Commented Sep 7, 2017 at 3:33
  • 2
    If message is bytes, you shouldn’t represent it with a string in JavaScript. Leave it as a Uint8Array. Commented Sep 7, 2017 at 3:34
  • do we talk about node or about browser JS here? You can't represent any byte array as string because not every byte array is valid unicode. Commented Sep 7, 2017 at 3:37
  • 1
    @DovBenyominSohacheski: How are you posting it to a server? Please show that code. Commented Sep 7, 2017 at 6:28
  • 1
    @DovBenyominSohacheski: stream.write accepts a buffer. Buffer.concat([Buffer.from([0x56, 0x54, 0x55, 0x3E, 0x28, 0x00, 0x08, 0x00, 0x03, 0x01, 0x46, 0x00, 0x00, 0x00, 0xC0]), checksum(data)]), where checksum also returns a Buffer. Commented Sep 7, 2017 at 6:30

1 Answer 1

2

With the help of @Ryan I was able to solve the issue by using a Buffer instead of an unsigned array.

Code

function checksum(data) {
    let res = 0x00
    for (let i = 0; i < data.length; ++i) {
        res = (res + data[i]) & 0xFF
    }
    return Buffer.from([res])
}

let data = Buffer.from([0x56, 0x54, 0x55, 0x3E, 0x28, 0x00, 
                  0x08, 0x00, 0x03, 0x01, 0x4F, 0x00, 0x00, 0x00])
console.log(Buffer.concat([data, checksum(data)]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.