TIP #275: Support Unsigned Values in binary Command


TIP:275
Title:Support Unsigned Values in binary Command
Version:$Revision: 1.5 $
Author:Pat Thoyts <patthoyts at users dot sourceforge dot net>
State:Final
Type:Project
Tcl-Version:8.5
Vote:Done
Created:Wednesday, 27 September 2006
Keywords:Tcl, binary, unsigned

Abstract

This TIP proposes to add support for unsigned values to the binary command.

Rationale

It is quite common when handling binary data to need to read unsigned values. All values read in by binary scan are converted to integers and sign extended, which requires code to trim the excess bits using the expr command if the values need to be treated as unsigned values. This makes Tcl slow when handling large quantities of binary data, for instance in crytographic or checksum functions. Simply by avoiding this expr operation we significantly improve the speed of processing such data.

Specification

The binary scan should be updated so as to add a flag identifier into the binary field format. This makes the field specification:

<type code><flag>?<length>?

The flag character for unsigned fields (the only one defined) is "u", so the following are all acceptable:

cu*

read all bytes as unsigned

c2cu2

two signed bytes then two unsigned bytes

su2

two unsigned little-endian shorts

Su

a single big-endian unsigned short.

iu

a single unsigned little-endian integer

mu3

three unsigned machine-endian 64-bit integers.

The "u" flag will be supported combined with all field types but will have no effect on non-integer types. Also the flag is valid but ignored for the binary format command.

Reference Implementation

A reference implementation is available in SourceForge Patch 156751 [1].

Copyright

This document is placed in the public domain.


Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows