Using "read" and "format" attribute together

I’m new to 010, and have absolutely fallen in love with binary templates. However, I ran into an issue that took me quite a bit of time to solve, and I’m wondering if I’m missing something.

I’m working with a format that has relative offsets, and it is significantly more helpful to me to see those as absolute offsets, so I wrote this function:

uint32 returnAbsoluteOffset() {
    return (startof(this) + 4) + this;
}

Which was supposed to get invoked like:

uint32 offset <read=returnAbsoluteOffset(), format=hex>;

But the format attribute doesn’t work; the value just gets displayed as decimal. The solution I ended up with was:

string returnAbsoluteOffset() {
    return Str("%Xh", (startof(this) + 4) + this);
}

And that works! But it’s uglier than what I originally wrote, I think. Is there a more elegant way to do this, or am I doing it the best way?

1 Like

You can also use this:

uint32 offset <read=Str("%Xh", (startof(this) + 4) + this)>; 

Note that when using the ‘read’ attribute, the ‘format’ attribute has no effect. If you want to use this definition multiple times you could use a typedef to create a separate type.

Graeme
SweetScape Software

1 Like

Yeah, I thought of making a typedef too. I think that may be the way to go here, thanks!