Re: [racket-users] Writing binary data

2016-10-18 Thread David Storrs
Nice.  Thanks, Tony.

On Tue, Oct 18, 2016 at 11:27 AM, Tony Garnock-Jones 
wrote:

> On 10/18/2016 12:27 AM, David Storrs wrote:
> > On Mon, Oct 17, 2016 at 8:39 PM, Sam Tobin-Hochstadt
> > mailto:sa...@cs.indiana.edu>> wrote:
> > I think the `integer->integer-bytes` function is probably what you
> want.
> >
> > Thanks Sam, that does the trick.
>
> You might also find the `bitsyntax` package useful:
> https://pkgn.racket-lang.org/package/bitsyntax
>
> Here's a snippet from an ELF image writer:
>
>   (bit-string ((hash-ref strtab-index name) :: little-endian bits 32)
>   ((symbol-scope->number scope) :: little-endian bits 4)
>   ((symbol-type->number type) :: little-endian bits 4)
>   (0 :: little-endian bits 8) ;; st_other, reserved
>   ((section->number section) :: little-endian bits 16)
>   (value :: little-endian bits 64)
>   (size :: little-endian bits 64))
>
> (There's `bit-string-case` for taking binary blobs apart, too.)
>
> Tony
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Writing binary data

2016-10-18 Thread Tony Garnock-Jones
On 10/18/2016 12:27 AM, David Storrs wrote:
> On Mon, Oct 17, 2016 at 8:39 PM, Sam Tobin-Hochstadt
> mailto:sa...@cs.indiana.edu>> wrote:
> I think the `integer->integer-bytes` function is probably what you want.
> 
> Thanks Sam, that does the trick. 

You might also find the `bitsyntax` package useful:
https://pkgn.racket-lang.org/package/bitsyntax

Here's a snippet from an ELF image writer:

  (bit-string ((hash-ref strtab-index name) :: little-endian bits 32)
  ((symbol-scope->number scope) :: little-endian bits 4)
  ((symbol-type->number type) :: little-endian bits 4)
  (0 :: little-endian bits 8) ;; st_other, reserved
  ((section->number section) :: little-endian bits 16)
  (value :: little-endian bits 64)
  (size :: little-endian bits 64))

(There's `bit-string-case` for taking binary blobs apart, too.)

Tony

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Writing binary data

2016-10-18 Thread Doug Williams
David pointed out that I failed to do a reply all when I pointed him to the
packed-binary package on PLaneT that does what he wants. Here is the text
of that post in case anyone else ever needs it.

I have a packed-binary package on PLaneT that we use for for reading binary
data based on a similar capability in Python. It can read or write from
byte strings or directly to/from binary files. It uses format strings to
define the data types to read:

Character

C Type

Racket

x

pad byte

no value

c

char

char

b

signed char

integer

B

unsigned char

integer

h

short

integer

H

unsigned short

integer

i

int

integer

I

unsigned int

integer

l

long

integer

L

unsigned long

integer

q

long long

integer

Q

unsigned long long

integer

f

float

real

d

double

real

s

char[]

string

and for byte order, size, and alignment:

Character

Byte Order

Size and Alignment

@

native

native

=

native

standard

<

little endian

standard

>

big endian

standard

!

network (big endian)

standard
The documentation is at https://planet.racket-lang.
org/package-source/williams/packed-binary.plt/1/5/planet-doc
s/packed-binary/index.html.

And the PLaneT entry is at https://planet.racket-lang.
org/display.ss?package=packed-binary.plt&owner=williams&changerep=2.

On Mon, Oct 17, 2016 at 6:38 PM, David Storrs 
wrote:

> What is the best way to write binary data such that I have control of the
> representation?  Basically, I want the Racket equivalent of the Perl 'pack
> ' and 'unpack
> '
> functions, where I can do:  pack('C', 202) and get back a one-byte binary
> representation of the decimal number 202 (or pack('s', 202) to get a 2-byte
> version) that I could then write to a port.
>
> The reason that I'm looking for this is that I'm going to be writing
> binary data across the network and I want to be able to control the
> representation.  For a fairly trivial example, I want to be able to decide
> how many bytes the number 202 should take up.[1]   Furthermore, I'd like to
> have an easy way to do quick and dirty testing; I thought that writing to a
> byte string would be that way, but I'm not having much success with it, as
> I haven't figured out how to make things go in as raw bytes instead of
> strings.
>
> I've looked at fasl, binary-class, the implementation of protobuf, and
> everything in the racket-lang docs that I could think of keywords for, with
> no luck.  This feels like it should be a straightforward task, so I'm
> frustrated with myself for not figuring it out more quickly.
>
>
> [1] I keep using the number 202 because that's the size of one particular
> message that I'm using in my text script and I'm trying to do RLE on it.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Writing binary data

2016-10-17 Thread David Storrs
On Mon, Oct 17, 2016 at 8:39 PM, Sam Tobin-Hochstadt 
wrote:

> I think the `integer->integer-bytes` function is probably what you want.
>
> Sam
>
>
Thanks Sam, that does the trick.


On Mon, Oct 17, 2016 at 8:38 PM, David Storrs 
> wrote:
> > What is the best way to write binary data such that I have control of the
> > representation?  Basically, I want the Racket equivalent of the Perl
> 'pack'
> > and 'unpack' functions, where I can do:  pack('C', 202) and get back a
> > one-byte binary representation of the decimal number 202 (or pack('s',
> 202)
> > to get a 2-byte version) that I could then write to a port.
> >
> > The reason that I'm looking for this is that I'm going to be writing
> binary
> > data across the network and I want to be able to control the
> representation.
> > For a fairly trivial example, I want to be able to decide how many bytes
> the
> > number 202 should take up.[1]   Furthermore, I'd like to have an easy
> way to
> > do quick and dirty testing; I thought that writing to a byte string
> would be
> > that way, but I'm not having much success with it, as I haven't figured
> out
> > how to make things go in as raw bytes instead of strings.
> >
> > I've looked at fasl, binary-class, the implementation of protobuf, and
> > everything in the racket-lang docs that I could think of keywords for,
> with
> > no luck.  This feels like it should be a straightforward task, so I'm
> > frustrated with myself for not figuring it out more quickly.
> >
> >
> > [1] I keep using the number 202 because that's the size of one particular
> > message that I'm using in my text script and I'm trying to do RLE on it.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Writing binary data

2016-10-17 Thread Sam Tobin-Hochstadt
I think the `integer->integer-bytes` function is probably what you want.

Sam

On Mon, Oct 17, 2016 at 8:38 PM, David Storrs  wrote:
> What is the best way to write binary data such that I have control of the
> representation?  Basically, I want the Racket equivalent of the Perl 'pack'
> and 'unpack' functions, where I can do:  pack('C', 202) and get back a
> one-byte binary representation of the decimal number 202 (or pack('s', 202)
> to get a 2-byte version) that I could then write to a port.
>
> The reason that I'm looking for this is that I'm going to be writing binary
> data across the network and I want to be able to control the representation.
> For a fairly trivial example, I want to be able to decide how many bytes the
> number 202 should take up.[1]   Furthermore, I'd like to have an easy way to
> do quick and dirty testing; I thought that writing to a byte string would be
> that way, but I'm not having much success with it, as I haven't figured out
> how to make things go in as raw bytes instead of strings.
>
> I've looked at fasl, binary-class, the implementation of protobuf, and
> everything in the racket-lang docs that I could think of keywords for, with
> no luck.  This feels like it should be a straightforward task, so I'm
> frustrated with myself for not figuring it out more quickly.
>
>
> [1] I keep using the number 202 because that's the size of one particular
> message that I'm using in my text script and I'm trying to do RLE on it.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.