On Fri, 22 Mar 2002, Taybin Rutkin wrote:

> How does one write a binary file in unicon?  As far as I can tell,
> everything is so high-level that it is difficult that you want to write a
> long followed by an integer or whatever.


You are right that Icon is not set up for this, but I have had
good luck by writing the obvious routines such as the one here.

Be sure to open your file in "untranslated" mode.  (If you have
been dealing with binary file, you probably figured that out
already.)

Also, be sure you understand that in most implementations of Icon
there are no long and short ints--they expand to hold arbitrarily
large numbers.  So if you want to write a "short" you write a two
byte version of this routine, or you take the bottom two
characters of the result, or whatever.

-- Michael Glass

 ____________________________________________________________
#
# s := i4chars(n)
#
# Convert the low 4 bytes of an integer to a 4-character string,
# so that each character of the string is one byte of the number.
#
#  4-byte input:    abcd
#                   |  |
#                   |  +-- Least significant byte
#                   +----- Most significant byte
#
#  4-char result string: "DCBA"
#                         |  |
#                         |  +-- char(a)
#                         +----- char(d)
#
#
# The reason for the seeming reversal of the bytes is so
# that when the string is written to the file, the least
# significant byte will be emitted first (i.e. "little endian")
#
#----------------------------------------------------------------- 
#
procedure i4chars(n)
   local rslt
   rslt := ""
   every 1 to 4 do {
      rslt := rslt || char(iand(n, 255)) 
      n := ishift(n, -8) 
   }
   return rslt
end
_____________________________________________________________________



_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to