0

I have a function which outputs the integer 128 as a character into a file. When I reopen this file to read that character with the next function, it reads the sequence of characters in the form 60 49 54 114 48 48 56 48 62. When I output 127 into the file and then read it again, next correctly returns 127, so what's wrong with the characters > 128?. How can I read the actual integer representation of the character correctly?

Code for outputting an integer into a file

|inputfile args name|
args := Smalltalk arguments.
name := args at: 1.
inputfile := FileStream open: name mode: FileStream write.
inputfile << 128 asCharacter.
inputfile close.

Code for reading a file

|inputfile args name|
    args := Smalltalk arguments.
    name := args at: 1.
    inputfile := FileStream open: name mode: FileStream read.
    [inputfile atEnd ~= true] whileTrue:[stdout << inputfile next asInteger.].
    inputfile close.
patzi
  • 353
  • 4
  • 13
  • GNU Smalltalk? According to the documentation for `Character`: *`Character` is always used (mostly for performance reasons) when referring to characters whose code point is between 0 and 127.* – lurker Nov 22 '17 at 02:29
  • Try: `inputfile nextPut: (Character value: 128)`. This forces the value `128` to be a normal character (not Unicode), and puts it to the file in binary. – lurker Nov 22 '17 at 02:40
  • @lurker My original problem is that I am working on a file compressor and although the uncompressed version of the file vs the original file look the same in terms of integer representation, the diff command keeps telling that the binary files are different. What might be the cause?. – patzi Nov 22 '17 at 02:40
  • The cause is that `128 asCharacter` represents a `UnicodeCharacter`, not a single byte binary (at the prompt, try `128 asCharacter class`). This is described in the GNU documentation. The `<<` selector further interprets that before sending to the file stream. You can use the code I wrote in my last comment to do what you want. – lurker Nov 22 '17 at 02:42
  • @lurker My teacher is the one who provides the testing files. I am not allowed to change them. – patzi Nov 22 '17 at 02:42
  • What do you mean by *testing files*? The code? If you can't change the code for outputting the data, then what recourse do you have? – lurker Nov 22 '17 at 02:44
  • Nevermind. It worked!!!!!!!!! – patzi Nov 22 '17 at 02:49

0 Answers0