The Rants, Raves, Gripes, and Prophecies of Paul R. Potts

Contents by Category

Contents by Date

Favorite Links

My Wiki: main entrance
Boing Boing
Gwydion Dylan
Paul Graham
Richard P. Gabriel

Wed, 06 Jul 2005 Handling SREC files with Ruby

Ruby is definitely one of my favorite languages, and I am far more comfortable with it than I am with Perl. It seems very expressive; very frequently, once I find the right library method, the code pretty much writes itself.

However, just like with Perl, there is more than one way to do it, so I often find myself looking for more appropriate idioms for the task at hand. There is also some question of efficiency; I'm not trying to optimize prematurely, but the program I'm working on runs slower than I'd like, and I think Ruby can do better.

To start, I'll just share one quick annoyance. The first is that there is some deficiency in handling of typing such that statements like

puts "Checksum: " + chksm

will not work; you get a run-time error (can't convert Fixnum into String)." This seems wrong; it is not very "DWIM" (do what I mean) when considered in light of Ruby's philosophy of weak "duck typing" -- if it quacks, for all practical purposes your program can treat it as a duck, or in this case a string. I bring this up because I keep getting this error -- I have a habit of forgetting to add .to_s to the variable. I'm thinking in terms of C++ iostreams, where the type is taken care of using the stream << operator.

But on to a meatier question. I want to be able to treat a string containing ASCII hex digits as an array of bytes or as a Fixnum (where I can specify the byte ordering). For example, I want to be able to turn

"DEADBEEF" into an array of unsigned integers [0xDE, 0xAD, 0xBE, 0xEF] (for purposes of generating a bytwise checksum), or into the unsigned integers 0xDEAD, 0xDEADBE, or 0xDEADBEEF (depending on the record type I'm working on).

These are the ASCII character values (exprssed in hex); 'D' is decimal 68, hex 44. Not very useful; I get 2 strings of ASCII hex out. These aren't very useful for translation, since I'd have to turn them into integers, translate them to numeric values (not just a simple offset, because the hex characters are not contiguous) and then assemble the high and low nybbles into byte values using (high << 4) + low, or some such.

There must be a better way to do this. Here's my first try:

def make_checksum (str)
  checksum = 0
  0.step(str.length - 1, 2) { |idx|
    checksum += (str[idx].chr.hex * 16) + (str[idx + 1].chr.hex)
  return (~checksum) & 0xFF

Ugh. Look at the way I have to access the characters: str[idx] returns numeric types, not character types, which don't have a hex() method, so I have to convert them from the original characters in the string to integers, to characters, and then apply hex() to that.

Another way is to make substrings, I suppose, but it doesn't perform better, probably because it is generating a lot of extra string objects:

def make_srec_checksum (str)
  puts "make_srec_checksum; str is: " + str
  checksum = 0
  0.step(str.length - 1, 2) { |idx|
    checksum += str[idx..(idx + 1)].hex
  return (~checksum) & 0xFF

Another is that the String.unpack() method seemed from reading the documentation that it had exactly what I needed; it appeared that the "h" and "H" characters in the control string for the unpack method would be capable of unpacking ASCII hex data like DEADBEEF into byte values. That seems to be what unpack is all about. Instead, it generates ASCII hex bytes.

$ ruby -e 'puts "0123456789ABCDEF".unpack("H2" * 16)'


Not what I wanted. Most likely, there is a much better way!

It might be interesting to compare Common Lisp and Ruby implementations of my SREC tool; how concise, yet expressive, can I be in each language? This also might serve to shed some light on whether Common Lisp has fallen behind as far as libraries for typical file-handling and scripting-type tasks. More on that later.

[/root/geeky/programming/ruby] permanent link

A Squeak in the Wilderness

Blah blah apologies blah blah long time since I've written blah. Blah busy.

I guess I'm not much of a blogger. I was especially chagrined when my brother told me he hadn't seen anything new in a long time. I didn't even realize he was reading! Hi, Brian!

There is lots of news with baby Veronica. She is eight months old. She weighs twenty-something pounds, has one tooth, can stand briefly, "cruises" (walks with support), climbs stairs unaided, blows rasberries as a form of conversation, wrestles, giggles, sings, and dances to the theme music from "Red Dwarf." Although her main food is breast milk, she loves to eat bits of asparagus, bananas, and little bits of any kind of bread or pasta. She does not say clearly recognizable words yet (although she sort of says "hi"), but she tries to imitate words we say. We have to do much more baby-proofing than we expected. Apparently Isaac did not require very much baby proofing, but Vera moves fast, and likes to pull things off shelves. She especially loves to tear up books and eat the pages.

My time at home is considerably shorter than it was before, since I'm spending upwards of two hours commuting each weekday. When I do get home, I am spending more time chasing the baby and trying to run interference for Grace. Getting a little uninterrupted time to sit in the office to write or program has become a luxury. Grace has taken the kids camping with her sister-in-law, so I have a few free evenings.

Much more has happened; Grace's brother Ben Benjamin died without warning, and we are still in shock about that. He had undiagnosed hypertension (high blood pressure). He leaves a wife and four children, one teen-aged, two toddlers, and one small baby who was born with a dangerous heart condition and who is, happily, recovering from surgery. It makes me think hard about what Grace would do with Veronica and Isaac if something similar should happen to me. We've seen a lawyer to get complete wills drafted and I just had my physical for a new life insurance policy.

I've been working for Visteon in Dearborn, doing software testing for a satellite radio product. I'm technically a contractor on a limited- time project; hence the commute. If it turns into a permanent position we might move to Dearborn.

Meanwhile, I've not been able to do much free-time coding. I have put together a little tool in Ruby to do some assorted processing on SREC files. The SREC format is used in embedded software to hold downloadable code segments. The original file format seems to be attributable to Motorola, although there seem to be a lot of variants floating around. It started out as a tool to scratch a personal itch: the need to merge multiple SREC files. If you have a particular problem you need to solve with SREC files, get in touch ( I'll talk a bit more about this program in another entry.

I had planned to do some work in Common Lisp and/or Scheme while Grace and the children were away, but it turns out that I procrastinated too long, since she cut her trip a week short to return to Ann Arbor for her brother's funeral. She is taking another trip in early July, though, so if I can avoid procrastination, perhaps I will have something to show. It might be interesting to compare Common Lisp and Ruby implementations of my SREC tool; how concise, yet expressive, can I be in each language? This also might serve to shed some light on whether Common Lisp has fallen behind as far as libraries for typical file-handling and scripting-type tasks.

I haven't written about the war in Iraq or other political issues in some time. It doesn't seem that very much has changed except the daily details.

By any fact-based measure, America is not succeeding. Even rational Republicans are wavering. But Bush did not offer any changes in strategy. Bush's prime-time address last night boiled down, basically, to more of the same: "there was some connection between 9/11 and Iraq," "we're winning," and "we're not leaving before we've won." Oh, and "we can continue to win this war on the cheap, without increasing troop strength, and without sacrificing the tax cuts." Also, that there is no viable plan to get more assistance from the U.N. or from other nations, and apparently no movement on the situation with detainees, hundreds of whom appear destined to remain imprisoned for perhaps years into the forseeable future without even the pretense of a trial.

It would be nice to hear some acknowledgement that this failure was predictable. But I don't want to gloat; I want to see lives saved, both Iraqui and American. This just can't be achieved by inflicting futher terrorism on a country that didn't, and wasn't capable of, attacking us.

My son is about to turn 11 years old; he'll be old enough to serve in the military in 7 years. My daughter, in just over 17 years. Will we still have troops in Iraq then? Will we have an economy in shape to provide them with any other prospects for employment? Will they be drafted?

Without leaders who are willing to acknowledge mistakes and change course, it doesn't look promising.

[/root/news] permanent link

Creative Commons License

Viewable With Any Browser