putting character in a stream

Tagged:
Project:GNU Smalltalk
Component:Base classes
Category:bug
Priority:normal
Assigned:Unassigned
Status:invalid
Description

Platform is Centos 5.2 64 bit version. GST 3.1.

This works...

               valueStream := ReadStream on: '7F'.
               stream := ReadWriteStream on: (String new).
               [valueStream atEnd] whileFalse: [
                       byte := (valueStream next digitValue * 16).
                       byte := byte + valueStream next digitValue.
                       byte printNl.
                       byte asCharacter ].

This works...

               valueStream := ReadStream on: '80'.   "change the number"
               stream := ReadWriteStream on: (String new).
               [valueStream atEnd] whileFalse: [
                       byte := (valueStream next digitValue * 16).
                       byte := byte + valueStream next digitValue.
                       byte printNl.
                       byte asCharacter ].

This works...

               valueStream := ReadStream on: '7F'.
               stream := ReadWriteStream on: (String new).
               [valueStream atEnd] whileFalse: [
                       byte := (valueStream next digitValue * 16).
                       byte := byte + valueStream next digitValue.
                       byte printNl.
                       stream nextPut: (byte asCharacter). ].   "save to a stream"

This crashes...

               valueStream := ReadStream on: '80'.    "change the number"
               stream := ReadWriteStream on: (String new).
               [valueStream atEnd] whileFalse: [
                       byte := (valueStream next digitValue * 16).
                       byte := byte + valueStream next digitValue.
                       byte printNl.
                       stream nextPut: (byte asCharacter). ].

The crash looks like this...

$ gst -i ./berintdebug.st...

128 
Object: '' error: Invalid argument : argument must be between $<0> and $ÿ
SystemExceptions.ArgumentOutOfRange(Exception)>>signal (AnsiExcept.st:216)
SystemExceptions.ArgumentOutOfRange(Exception)>>signal: (AnsiExcept.st:226)
SystemExceptions.ArgumentOutOfRange class>>signalOn:mustBeBetween:and:  (AnsiExcept.st:780)
String(Object)>>checkIndexableBounds:put: (Object.st:805)
String>>at:put: (String.st:293)
ReadWriteStream(WriteStream)>>nextPut: (WriteStream.st:93)
UndefinedObject>>executeStatements (berintdebug.st:12)

By the way, I expect there is a better way of converting from hex to decimal and putting into a stream...

Also the code below crashes the terminal window.

 Transcript show: myObject class. 

Updates

#1 submitted by Paolo Bonzini on Thu, 03/05/2009 - 09:47
Status:active» invalid

The problem is that asCharacter (by design) returns a UnicodeCharacter when given a character that is >128. You need to use (Character value: byte) to explicitly request a Character:

st> (Character value: 128) class
Character
st> (Character codePoint: 128) class
UnicodeCharacter
st> 128 asCharacter class
UnicodeCharacter

For ease of use, Characters and UnicodeCharacters are the same for the range 0-127. This works because sane representations of Unicode are backwards compatible with 7-bit ASCII, but does not work for the range 128-255. A Character in the range 128-255 is the octet 128-255 stored in a representation of a Unicode string (i.e. in a String), while a UnicodeCharacter in the range 128-255 is the character 128-255 of the Unicode character set.

I'll open another issue for the Transcript deadlock.

User login