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
Answers
Ok, the solution was quite easy...
Just cast data to Int8Array and now its working fine..
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.Also, Please note that
String.fromCharCode
does not accept array as argument. therefore, should be done like following:So, If you implement this workaround in your code, It would look like this: