How can i convert signed int in to char in js?

74 views Asked by At

I'm trying to pass a png file from my server (cpp) to a socket (js) by numbers (each number from -128 to 127 is a range of standard characters in cpp) but if I use the String.fromCharCode() method - this won't work

Output from String.fromCharCode is just different from char conversion on cpp (for negative numbers), are there other methods in js that can help me?..

Code on client (js):

socket.onmessage = function (event) {
                const splitted = event.data.split("|")
                if (splitted[0] === "file -d") {
                    let data = splitted[2].split(",")
                    data = String.fromCharCode(...data)
                    console.log(data)
                    const link = document.createElement("a")
                    const file = new Blob([data], { type: 'application/octet-stream;charset=utf-8' })
                    link.href = URL.createObjectURL(file)
                    link.download = splitted[1]
                    link.click()
                    URL.revokeObjectURL(link.href)
                } else
                    console.log("Received from the server: \"" + event.data + "\"")
            }

Code on server (cpp):

connection->send("file -d|" + "my.png" + "|" + "-119,80,78,71,13,...); // just binary data but char-->int

Code for sending file from client to server (js):

submitFile.onmousedown = function () {
        if (connected === true) {
            let file = fileStorage.files[0]
            let fileBuffer = new ArrayBuffer(0)
            let fileReader = new FileReader()
            fileReader.onloadend = function (event) {
                fileBuffer = event.target.result
                let fileData = new Int8Array(fileBuffer)
                socket.send("file -u|" + file.name + "|" + "./files|" + file.size + "|" + fileData)
            }
            fileReader.readAsArrayBuffer(file)
        } else
            alert("No connection to the server")
    }
2

There are 2 answers

1
Svyatoslav Kuralenok On BEST ANSWER

Ok, the solution was quite easy...

socket.onmessage = function (event) {
                const splitted = event.data.split("|")
                if (splitted[0] === "file -d") {
                    let data = new Int8Array(splitted[2].split(","))
                    console.log(data)
                    const link = document.createElement("a")
                    const file = new Blob([data], {type: 'application/octet-stream;charset=utf-8'})
                    link.href = URL.createObjectURL(file)
                    link.download = splitted[1]
                    link.click()
                    URL.revokeObjectURL(link.href)
                } else
                    console.log("Received from the server: \"" + event.data + "\"")
            }

Just cast data to Int8Array and now its working fine..

1
Alex Park On

It seems you are trying to parse file -d execution output. and your output seems to be providing with signed char type formatted as decimal instead of unsigned ones.

In this case, I would emulate a byte underflow to get (0~255) counterpart of the negative part.

// adding n since n is negative and will wrap around
const converted = n < 0 ? 256 + parseInt(n, 10) : parseInt(n, 10);

Also, Please note that String.fromCharCode does not accept array as argument. therefore, should be done like following:

// individually convert character and concatenate with .reduce function
const convertedString = charArray.map(char => String.fromCharCode(char)).reduce((a,b) => a+b);

So, If you implement this workaround in your code, It would look like this:

socket.onmessage = function (event) {
                const splitted = event.data.split("|")
                if (splitted[0] === "file -d") {
                    let data = splitted[2].split(",")
                    data = data
                        .map(n => String.fromCharCode(n < 0 ? 256+parseInt(n, 10) : parseInt(n, 10)))
                        .reduce((a,b) => a+b)
                    console.log(data)
                    const link = document.createElement("a")
                    const file = new Blob([data], { type: 'application/octet-stream;charset=utf-8' })
                    link.href = URL.createObjectURL(file)
                    link.download = splitted[1]
                    link.click()
                    URL.revokeObjectURL(link.href)
                } else
                    console.log("Received from the server: \"" + event.data + "\"")
            }