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 <david.sto...@gmail.com> 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 > <http://perldoc.perl.org/functions/pack.html>' and 'unpack > <http://perldoc.perl.org/functions/unpack.html#pack-TEMPLATE%2cLIST>' > 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.