I'm current开发者_如何学运维ly working on a piece of software in Ruby that is meant to read a binary message from a file, and then transmit it over either a TCP or UDP socket to a C program being written by a coworker of mine. This C program must be able to perform bitwise operations on these binary messages, before sending them back to my program to compare the sent and received data.
My current issue relates to the way Ruby seems to deal with everything as strings. I am relatively new to the language and am unsure about how i should approach this problem.
My main concern is making sure that no changes occur to the content of my binary messages before, or during their initial transfer to the C program.
Any help you could provide would be greatly appreciated.
Thanks, Martin
Ruby's Class: IO is a good starting point. Read the first section, particularly the "b"
flag and how it relates to OSes. After that read
or read_bytes
might be useful.
Re: "Ruby seems to deal with everything as strings". Ruby sees a file as a sequence of bytes read from disk. It's how you tell Ruby to read, process and display those bytes that determines whether the bytes are lines of text, records from some database file, an image or sound file. You can read text using data-oriented methods, then turn around and treat the data as a string. You could read "binary" data using string-reading methods then aggregate it in memory and treat it as bytes. Ruby is just the tool we use to manipulate the bytes but we're the ones who decide what those bytes mean.
精彩评论