Ian McInnes
Ian McInnes

Reputation: 29

How to read text file strictly as 256 character ASCII

I am trying to read a game data file from an old windows RPG in order to create an editor program. The code reads each character as its ASCII value. The files are in 256 character ASCII, but when I try to read the file in Node.js, some of the characters fall out of range. Some of the int values of the characters read include '1206', '1268', '1674', '65533', etc.

How do I make the code read each character of the file strictly as numbers 0 to 255, as they are supposed to be? (As a side note, I would also like to know how to code this in Python as well)


const fs = require('node:fs');
fs.readFile('OUTDOOR.DAT', 'utf8', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }
  const dataLen = data.length;
  let chars = [];
  for (let i = 0; i < dataLen; i++) {
    chars.push(data.charCodeAt(i));
  }
  console.log(chars);
});

So, for example, the characters 'ÿÿÿ°' should be read as [255, 255, 255, 176] but are all read as 65533 when Node reads it from the file. If I paste those characters into a string and ask Node to convert that, it comes up the way I need.

Is it the 'utf8' that throws it off?

Upvotes: -1

Views: 42

Answers (1)

mwopitz
mwopitz

Reputation: 508

Sounds like you don't really want to read OUTDOOR.DAT as text characters. I imagine this file contains some kind of binary game data, like a level or save. The file does not contain ASCII text because "ÿÿÿ°" are not ASCII characters and do not look like human-readable text to me. "ÿÿÿ°" is what a text editor like Windows Notepad will display when reading the byte sequence with decimal values [255, 255, 255, 176] as ANSI-encoded text.

Just call fs.readFile without explicit encoding argument. It gives you a raw buffer with the byte values you want:

fs.readFile('OUTDOOR.DAT', (err, data) => {
  if (err) {
    console.error(err);
    return;
  }

  for (const byte of data) {
    console.log(byte);
  }
});

Upvotes: 2

Related Questions