Search code examples
javascriptwebsocketfromcharcode

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


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")
    }

Solution

  • 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..