Hi Stan
At the present time, REBOL does not directly support these sorts of
operations. However, it is possible to do it using REBOL. I don't really
have the time to write all of the pieces up, but these comments may get you
started.
1) REBOL's binary! datatype is the closest thing to your "Smalltalk
ByteArray".
2) REBOL does not support 2-byte integers or unsigned integers, only 4 byte
signed integers.
3) You do not specify whether the binary integers need to be byte-reversed
or not. Intel, Motorola 68xxx, and some others require the bytes in each
binary numeric type to be reversed, other machines do not.
4) I don't understand your last entry "12 byte numeric field"?
I have included a small function which will translate REBOL integers into
byte-reversed 2-or-4 byte binary values.
Here is a simple hand calculation example of how to use this function in
conjunction with the binary! datatype which will (hopefully?) work for your
example. I have assumed you want the numbers byte-reversed, if not remove
the "reverse" from TO-BYTE. Notice the binary is displayed in hex digits, so
2 digits/byte.
>> bin: to-byte 694
== #{B602}
>> append bin to-byte/long 251
== #{B602FB000000}
>> append bin to-byte 1
== #{B602FB0000000100}
>> append bin to-binary " 2000-18-15.01.57.000000"
== #{
B602FB00000001002020202020323030302D31382D31352E30312E35372E3030
30303030
}
>> head insert/dup tail bin to-byte 0 6
== #{
B602FB00000001002020202020323030302D31382D31352E30312E35372E3030
30303030000000000000000000000000
}
;write/binary my-port bin
;or
;insert my-port bin ;port should set up for binary I/O
=====TO-BYTE FUNCTION======
to-byte: func [int [integer!] /long][
head reverse do join "#{" [(copy skip (to-string to-hex int) either
long [0][4]) "}"]
]
Note: there is no checking for sign, so TO-BYTE will only work properly for
unsigned 2-byte up to 32767 and unsigned 4-byte up to 2147483647. It is
fairly easy to add code to allow REBOL negative integers to be
re-interpreted as the high-half unsigned's.
Someday I will make a whole package for binary signed/unsigned integers, but
no time now. I also have a function for extracting a REBOL integer from a
2/4 byte binary value. Let me know if you need it.
Hope this helps
Cheers
Larry
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, February 29, 2000 5:53 AM
Subject: [REBOL] Sending bytes to TCP port?
> Hi,
>
> I need to send a sequence of 694 bytes to a TCP port. My quetion is: How
do
> I collect these bytes? (In Smalltalk I would use a ByteArray).
>
> I need to send, for example:
>
> 694 as unsigned short (2 bytes)
> 251 as unsigned long (4 bytes)
> 1 as unsigned short (2 bytes)
> ...
> "2000-02-18-15.01.57.000000", padded left with spaces,
> as 28 byte character field
> ...
> 0, padded left with 0's, as 12 byte numeric field
> ...
>
> My first reaction is to write functions like the following, but I don't
know
> what to use for the byte-stream. Suggestions for other approaches also
> welcome.
>
> add-u-long func [
> integer [integer!] byte-stream [????]
> /local byte
> ][
> for shift-amount -24 0 8 [
> byte: bit-and (bit-shift integer shift-amount) 255
> append byte-stream byte
> ]
>
> Thanks in advance,
>
> Stan