image-js / iobuffer

Read and write binary data in ArrayBuffers
http://image-js.github.io/iobuffer/
MIT License
43 stars 6 forks source link

Question: IOBuffer.readChar behaviour #64

Closed ghost closed 1 year ago

ghost commented 1 year ago

The function readChar uses String.fromCharCode(buffer.readInt8()), so numbers above 127 return non ASCII characters.

Maybe this was chosen as the best option compared to other options.

Maybe grabbing the first 7 bits buffer.readUint8 & 127 or erroring out when it is off the boundaries would be an expected behaviour? Or maybe adding an argument in the function to either be flexible to 256 chars (extended ascii) or force 127 chars (erroring out when off boundaries).

lpatiny commented 1 year ago

Quite problematic indeed because a character does not have always the same length. We could think about specifying the encoding or we could expect it is latin-1

@santimirandarp I guess if you ask the question you have this issue. What is the encoding of the text ? A common problem in the °C. The symbol ° is over 127. ± is also problematic.

const a=Uint8Array.from([177,56,176,67]);
const result = (new TextDecoder('latin1')).decode(a);
console.log(result); // ±8°C
ghost commented 1 year ago

Thanks for the explanation. There is already IOBuffer.decodeText, maybe both readChar and readChars can be removed.

What do you think?