Binary pattern matching a float value

I’m trying to pattern match a binary file where the second value is a float. For example:

<< "JPEG", version :: size(5), rest :: binary >>

The version should be something like 01.50.

All I’m getting back though is version = 6.

Is there a special way to match on float values?

2 Likes

That’s not a float. They usually have a size of 32, 64 or 128 bit, not 5. I do think that you really want to have 5 bytes instead of bits and then just handle them as a string.

3 Likes

I think you’re right. The spec says the version is five characters, and the example is 01.50

1 Like

The term characters is a very strong hint for a string type.

1 Like

That’s what I thought when I first read it, but then the rest of the spec continues to use characters even when they’re just digits. That was only the second term in the spec, so I’m guessing everything is a string based off that.

1 Like

The word digit means a number from 0 to 9, which is most of the time encoded in of three ways.

  • a byte that just represents the digit. Wasting much space of the byte
  • a half byte, this way you can encode two digits into a single byte
  • the ASCII representation of the digit. While wasting space again this is easily inspectable with a lot of tools.

So unless specified as integer or float including it’s width and endianness, it’s almost everytime just characters.

1 Like

I’ve got another question, but I’m going to ask it in another thread. Perhaps if someone can answer, it may help other people.

1 Like