| 1 |
'\" |
| 2 |
'\" Copyright (c) 1997 Sun Microsystems, Inc. |
| 3 |
'\" Copyright (c) 2008 Donal K. Fellows |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH binary n 8.0 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
binary \- Insert and extract fields from binary strings |
| 14 |
.SH SYNOPSIS |
| 15 |
.VS 8.6 |
| 16 |
\fBbinary decode \fIformat\fR ?\fI\-option value ...\fR? \fIdata\fR |
| 17 |
.br |
| 18 |
\fBbinary encode \fIformat\fR ?\fI\-option value ...\fR? \fIdata\fR |
| 19 |
.br |
| 20 |
.VE 8.6 |
| 21 |
\fBbinary format \fIformatString \fR?\fIarg arg ...\fR? |
| 22 |
.br |
| 23 |
\fBbinary scan \fIstring formatString \fR?\fIvarName varName ...\fR? |
| 24 |
.BE |
| 25 |
.SH DESCRIPTION |
| 26 |
.PP |
| 27 |
This command provides facilities for manipulating binary data. The |
| 28 |
subcommand \fBbinary format\fR creates a binary string from normal |
| 29 |
Tcl values. For example, given the values 16 and 22, on a 32-bit |
| 30 |
architecture, it might produce an 8-byte binary string consisting of |
| 31 |
two 4-byte integers, one for each of the numbers. The subcommand |
| 32 |
\fBbinary scan\fR, does the opposite: it extracts data |
| 33 |
from a binary string and returns it as ordinary Tcl string values. |
| 34 |
.VS 8.6 |
| 35 |
The \fBbinary encode\fR and \fBbinary decode\fR subcommands convert |
| 36 |
binary data to or from string encodings such as base64 (used in MIME |
| 37 |
messages for example). |
| 38 |
.VE 8.6 |
| 39 |
.PP |
| 40 |
Note that other operations on binary data, such as taking a subsequence of it, |
| 41 |
getting its length, or reinterpreting it as a string in some encoding, are |
| 42 |
done by other Tcl commands (respectively \fBstring range\fR, |
| 43 |
\fBstring length\fR and \fBencoding convertfrom\fR in the example cases). A |
| 44 |
binary string in Tcl is merely one where all the characters it contains are in |
| 45 |
the range \eu0000\-\eu00FF. |
| 46 |
.SH "BINARY ENCODE AND DECODE" |
| 47 |
.VS 8.6 |
| 48 |
.PP |
| 49 |
When encoding binary data as a readable string, the starting binary data is |
| 50 |
passed to the \fBbinary encode\fR command, together with the name of the |
| 51 |
encoding to use and any encoding-specific options desired. Data which has been |
| 52 |
encoded can be converted back to binary form using \fBbinary decode\fR. The |
| 53 |
following formats and options are supported. |
| 54 |
.TP |
| 55 |
\fBbase64\fR |
| 56 |
. |
| 57 |
The \fBbase64\fR binary encoding is commonly used in mail messages and XML |
| 58 |
documents, and uses mostly upper and lower case letters and digits. It has the |
| 59 |
distinction of being able to be rewrapped arbitrarily without losing |
| 60 |
information. |
| 61 |
.RS |
| 62 |
.PP |
| 63 |
During encoding, the following options are supported: |
| 64 |
.TP |
| 65 |
\fB\-maxlen \fIlength\fR |
| 66 |
. |
| 67 |
Indicates that the output should be split into lines of no more than |
| 68 |
\fIlength\fR characters. By default, lines are not split. |
| 69 |
.TP |
| 70 |
\fB\-wrapchar \fIcharacter\fR |
| 71 |
. |
| 72 |
Indicates that, when lines are split because of the \fB\-maxlen\fR option, |
| 73 |
\fIcharacter\fR should be used to separate lines. By default, this is a |
| 74 |
newline character, |
| 75 |
.QW \en . |
| 76 |
.PP |
| 77 |
During decoding, the following options are supported: |
| 78 |
.TP |
| 79 |
\fB\-strict\fR |
| 80 |
. |
| 81 |
Instructs the decoder to throw an error if it encounters any characters |
| 82 |
that are not strictly part of the encoding itself. Otherwise it ignores them. |
| 83 |
RFC 2045 calls for base64 decoders to be non-strict. |
| 84 |
.RE |
| 85 |
.TP |
| 86 |
\fBhex\fR |
| 87 |
. |
| 88 |
The \fBhex\fR binary encoding converts each byte to a pair of hexadecimal |
| 89 |
digits that represent the byte value as a hexadecimal integer. |
| 90 |
When encoding, lower characters are used. |
| 91 |
When decoding, upper and lower characters are accepted. |
| 92 |
.RS |
| 93 |
.PP |
| 94 |
No options are supported during encoding. During decoding, the following |
| 95 |
options are supported: |
| 96 |
.TP |
| 97 |
\fB\-strict\fR |
| 98 |
. |
| 99 |
Instructs the decoder to throw an error if it encounters whitespace characters. |
| 100 |
Otherwise it ignores them. |
| 101 |
.RE |
| 102 |
.TP |
| 103 |
\fBuuencode\fR |
| 104 |
. |
| 105 |
The \fBuuencode\fR binary encoding used to be common for transfer of data |
| 106 |
between Unix systems and on USENET, but is less common these days, having been |
| 107 |
largely superseded by the \fBbase64\fR binary encoding. |
| 108 |
.RS |
| 109 |
.PP |
| 110 |
During encoding, the following options are supported (though changing them may |
| 111 |
produce files that other implementations of decoders cannot process): |
| 112 |
.TP |
| 113 |
\fB\-maxlen \fIlength\fR |
| 114 |
. |
| 115 |
Indicates the maximum number of characters to produce for each encoded line. |
| 116 |
The valid range is 5 to 85. Line lengths outside that range cannot be |
| 117 |
accommodated by the encoding format. The default value is 61. |
| 118 |
.TP |
| 119 |
\fB\-wrapchar \fIcharacter\fR |
| 120 |
. |
| 121 |
Indicates the character(s) to use to mark the end of each encoded line. |
| 122 |
Acceptable values are a sequence of zero or more characters from the |
| 123 |
set { \\x09 (TAB), \\x0B (VT), \\x0C (FF), \\x0D (CR) } followed |
| 124 |
by zero or one newline \\x0A (LF). Any other values are rejected because |
| 125 |
they would generate encoded text that could not be decoded. The default value |
| 126 |
is a single newline. |
| 127 |
.PP |
| 128 |
During decoding, the following options are supported: |
| 129 |
.TP |
| 130 |
\fB\-strict\fR |
| 131 |
. |
| 132 |
Instructs the decoder to throw an error if it encounters anything |
| 133 |
outside of the standard encoding format. Without this option, the |
| 134 |
decoder tolerates some deviations, mostly to forgive reflows of lines |
| 135 |
between the encoder and decoder. |
| 136 |
.PP |
| 137 |
Note that neither the encoder nor the decoder handle the header and footer of |
| 138 |
the uuencode format. |
| 139 |
.RE |
| 140 |
.VE 8.6 |
| 141 |
.SH "BINARY FORMAT" |
| 142 |
.PP |
| 143 |
The \fBbinary format\fR command generates a binary string whose layout |
| 144 |
is specified by the \fIformatString\fR and whose contents come from |
| 145 |
the additional arguments. The resulting binary value is returned. |
| 146 |
.PP |
| 147 |
The \fIformatString\fR consists of a sequence of zero or more field |
| 148 |
specifiers separated by zero or more spaces. Each field specifier is |
| 149 |
a single type character followed by an optional flag character followed |
| 150 |
by an optional numeric \fIcount\fR. |
| 151 |
Most field specifiers consume one argument to obtain the value to be |
| 152 |
formatted. The type character specifies how the value is to be |
| 153 |
formatted. The \fIcount\fR typically indicates how many items of the |
| 154 |
specified type are taken from the value. If present, the \fIcount\fR |
| 155 |
is a non-negative decimal integer or \fB*\fR, which normally indicates |
| 156 |
that all of the items in the value are to be used. If the number of |
| 157 |
arguments does not match the number of fields in the format string |
| 158 |
that consume arguments, then an error is generated. The flag character |
| 159 |
is ignored for \fBbinary format\fR. |
| 160 |
.PP |
| 161 |
Here is a small example to clarify the relation between the field |
| 162 |
specifiers and the arguments: |
| 163 |
.CS |
| 164 |
\fBbinary format\fR d3d {1.0 2.0 3.0 4.0} 0.1 |
| 165 |
.CE |
| 166 |
.PP |
| 167 |
The first argument is a list of four numbers, but because of the count |
| 168 |
of 3 for the associated field specifier, only the first three will be |
| 169 |
used. The second argument is associated with the second field |
| 170 |
specifier. The resulting binary string contains the four numbers 1.0, |
| 171 |
2.0, 3.0 and 0.1. |
| 172 |
.PP |
| 173 |
Each type-count pair moves an imaginary cursor through the binary |
| 174 |
data, storing bytes at the current position and advancing the cursor |
| 175 |
to just after the last byte stored. The cursor is initially at |
| 176 |
position 0 at the beginning of the data. The type may be any one of |
| 177 |
the following characters: |
| 178 |
.IP \fBa\fR 5 |
| 179 |
Stores a byte string of length \fIcount\fR in the output string. |
| 180 |
Every character is taken as modulo 256 (i.e. the low byte of every |
| 181 |
character is used, and the high byte discarded) so when storing |
| 182 |
character strings not wholly expressible using the characters \eu0000-\eu00ff, |
| 183 |
the \fBencoding convertto\fR command should be used first to change |
| 184 |
the string into an external representation |
| 185 |
if this truncation is not desired (i.e. if the characters are |
| 186 |
not part of the ISO 8859\-1 character set.) |
| 187 |
If \fIarg\fR has fewer than \fIcount\fR bytes, then additional zero |
| 188 |
bytes are used to pad out the field. If \fIarg\fR is longer than the |
| 189 |
specified length, the extra characters will be ignored. If |
| 190 |
\fIcount\fR is \fB*\fR, then all of the bytes in \fIarg\fR will be |
| 191 |
formatted. If \fIcount\fR is omitted, then one character will be |
| 192 |
formatted. For example, |
| 193 |
.RS |
| 194 |
.CS |
| 195 |
\fBbinary format\fR a7a*a alpha bravo charlie |
| 196 |
.CE |
| 197 |
will return a string equivalent to \fBalpha\e000\e000bravoc\fR, |
| 198 |
.CS |
| 199 |
\fBbinary format\fR a* [encoding convertto utf-8 \eu20ac] |
| 200 |
.CE |
| 201 |
will return a string equivalent to \fB\e342\e202\e254\fR (which is the |
| 202 |
UTF-8 byte sequence for a Euro-currency character) and |
| 203 |
.CS |
| 204 |
\fBbinary format\fR a* [encoding convertto iso8859-15 \eu20ac] |
| 205 |
.CE |
| 206 |
will return a string equivalent to \fB\e244\fR (which is the ISO |
| 207 |
8859\-15 byte sequence for a Euro-currency character). Contrast these |
| 208 |
last two with: |
| 209 |
.CS |
| 210 |
\fBbinary format\fR a* \eu20ac |
| 211 |
.CE |
| 212 |
which returns a string equivalent to \fB\e254\fR (i.e. \fB\exac\fR) by |
| 213 |
truncating the high-bits of the character, and which is probably not |
| 214 |
what is desired. |
| 215 |
.RE |
| 216 |
.IP \fBA\fR 5 |
| 217 |
This form is the same as \fBa\fR except that spaces are used for |
| 218 |
padding instead of nulls. For example, |
| 219 |
.RS |
| 220 |
.CS |
| 221 |
\fBbinary format\fR A6A*A alpha bravo charlie |
| 222 |
.CE |
| 223 |
will return \fBalpha bravoc\fR. |
| 224 |
.RE |
| 225 |
.IP \fBb\fR 5 |
| 226 |
Stores a string of \fIcount\fR binary digits in low-to-high order |
| 227 |
within each byte in the output string. \fIArg\fR must contain a |
| 228 |
sequence of \fB1\fR and \fB0\fR characters. The resulting bytes are |
| 229 |
emitted in first to last order with the bits being formatted in |
| 230 |
low-to-high order within each byte. If \fIarg\fR has fewer than |
| 231 |
\fIcount\fR digits, then zeros will be used for the remaining bits. |
| 232 |
If \fIarg\fR has more than the specified number of digits, the extra |
| 233 |
digits will be ignored. If \fIcount\fR is \fB*\fR, then all of the |
| 234 |
digits in \fIarg\fR will be formatted. If \fIcount\fR is omitted, |
| 235 |
then one digit will be formatted. If the number of bits formatted |
| 236 |
does not end at a byte boundary, the remaining bits of the last byte |
| 237 |
will be zeros. For example, |
| 238 |
.RS |
| 239 |
.CS |
| 240 |
\fBbinary format\fR b5b* 11100 111000011010 |
| 241 |
.CE |
| 242 |
will return a string equivalent to \fB\ex07\ex87\ex05\fR. |
| 243 |
.RE |
| 244 |
.IP \fBB\fR 5 |
| 245 |
This form is the same as \fBb\fR except that the bits are stored in |
| 246 |
high-to-low order within each byte. For example, |
| 247 |
.RS |
| 248 |
.CS |
| 249 |
\fBbinary format\fR B5B* 11100 111000011010 |
| 250 |
.CE |
| 251 |
will return a string equivalent to \fB\exe0\exe1\exa0\fR. |
| 252 |
.RE |
| 253 |
.IP \fBH\fR 5 |
| 254 |
Stores a string of \fIcount\fR hexadecimal digits in high-to-low |
| 255 |
within each byte in the output string. \fIArg\fR must contain a |
| 256 |
sequence of characters in the set |
| 257 |
.QW 0123456789abcdefABCDEF . |
| 258 |
The resulting bytes are emitted in first to last order with the hex digits |
| 259 |
being formatted in high-to-low order within each byte. If \fIarg\fR |
| 260 |
has fewer than \fIcount\fR digits, then zeros will be used for the |
| 261 |
remaining digits. If \fIarg\fR has more than the specified number of |
| 262 |
digits, the extra digits will be ignored. If \fIcount\fR is |
| 263 |
\fB*\fR, then all of the digits in \fIarg\fR will be formatted. If |
| 264 |
\fIcount\fR is omitted, then one digit will be formatted. If the |
| 265 |
number of digits formatted does not end at a byte boundary, the |
| 266 |
remaining bits of the last byte will be zeros. For example, |
| 267 |
.RS |
| 268 |
.CS |
| 269 |
\fBbinary format\fR H3H*H2 ab DEF 987 |
| 270 |
.CE |
| 271 |
will return a string equivalent to \fB\exab\ex00\exde\exf0\ex98\fR. |
| 272 |
.RE |
| 273 |
.IP \fBh\fR 5 |
| 274 |
This form is the same as \fBH\fR except that the digits are stored in |
| 275 |
low-to-high order within each byte. This is seldom required. For example, |
| 276 |
.RS |
| 277 |
.CS |
| 278 |
\fBbinary format\fR h3h*h2 AB def 987 |
| 279 |
.CE |
| 280 |
will return a string equivalent to \fB\exba\ex00\exed\ex0f\ex89\fR. |
| 281 |
.RE |
| 282 |
.IP \fBc\fR 5 |
| 283 |
Stores one or more 8-bit integer values in the output string. If no |
| 284 |
\fIcount\fR is specified, then \fIarg\fR must consist of an integer |
| 285 |
value. If \fIcount\fR is specified, \fIarg\fR must consist of a list |
| 286 |
containing at least that many integers. The low-order 8 bits of each integer |
| 287 |
are stored as a one-byte value at the cursor position. If \fIcount\fR |
| 288 |
is \fB*\fR, then all of the integers in the list are formatted. If the |
| 289 |
number of elements in the list is greater |
| 290 |
than \fIcount\fR, then the extra elements are ignored. For example, |
| 291 |
.RS |
| 292 |
.CS |
| 293 |
\fBbinary format\fR c3cc* {3 -3 128 1} 260 {2 5} |
| 294 |
.CE |
| 295 |
will return a string equivalent to |
| 296 |
\fB\ex03\exfd\ex80\ex04\ex02\ex05\fR, whereas |
| 297 |
.CS |
| 298 |
\fBbinary format\fR c {2 5} |
| 299 |
.CE |
| 300 |
will generate an error. |
| 301 |
.RE |
| 302 |
.IP \fBs\fR 5 |
| 303 |
This form is the same as \fBc\fR except that it stores one or more |
| 304 |
16-bit integers in little-endian byte order in the output string. The |
| 305 |
low-order 16-bits of each integer are stored as a two-byte value at |
| 306 |
the cursor position with the least significant byte stored first. For |
| 307 |
example, |
| 308 |
.RS |
| 309 |
.CS |
| 310 |
\fBbinary format\fR s3 {3 -3 258 1} |
| 311 |
.CE |
| 312 |
will return a string equivalent to |
| 313 |
\fB\ex03\ex00\exfd\exff\ex02\ex01\fR. |
| 314 |
.RE |
| 315 |
.IP \fBS\fR 5 |
| 316 |
This form is the same as \fBs\fR except that it stores one or more |
| 317 |
16-bit integers in big-endian byte order in the output string. For |
| 318 |
example, |
| 319 |
.RS |
| 320 |
.CS |
| 321 |
\fBbinary format\fR S3 {3 -3 258 1} |
| 322 |
.CE |
| 323 |
will return a string equivalent to |
| 324 |
\fB\ex00\ex03\exff\exfd\ex01\ex02\fR. |
| 325 |
.RE |
| 326 |
.IP \fBt\fR 5 |
| 327 |
This form (mnemonically \fItiny\fR) is the same as \fBs\fR and \fBS\fR |
| 328 |
except that it stores the 16-bit integers in the output string in the |
| 329 |
native byte order of the machine where the Tcl script is running. |
| 330 |
To determine what the native byte order of the machine is, refer to |
| 331 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 332 |
.IP \fBi\fR 5 |
| 333 |
This form is the same as \fBc\fR except that it stores one or more |
| 334 |
32-bit integers in little-endian byte order in the output string. The |
| 335 |
low-order 32-bits of each integer are stored as a four-byte value at |
| 336 |
the cursor position with the least significant byte stored first. For |
| 337 |
example, |
| 338 |
.RS |
| 339 |
.CS |
| 340 |
\fBbinary format\fR i3 {3 -3 65536 1} |
| 341 |
.CE |
| 342 |
will return a string equivalent to |
| 343 |
\fB\ex03\ex00\ex00\ex00\exfd\exff\exff\exff\ex00\ex00\ex01\ex00\fR |
| 344 |
.RE |
| 345 |
.IP \fBI\fR 5 |
| 346 |
This form is the same as \fBi\fR except that it stores one or more one |
| 347 |
or more 32-bit integers in big-endian byte order in the output string. |
| 348 |
For example, |
| 349 |
.RS |
| 350 |
.CS |
| 351 |
\fBbinary format\fR I3 {3 -3 65536 1} |
| 352 |
.CE |
| 353 |
will return a string equivalent to |
| 354 |
\fB\ex00\ex00\ex00\ex03\exff\exff\exff\exfd\ex00\ex01\ex00\ex00\fR |
| 355 |
.RE |
| 356 |
.IP \fBn\fR 5 |
| 357 |
This form (mnemonically \fInumber\fR or \fInormal\fR) is the same as |
| 358 |
\fBi\fR and \fBI\fR except that it stores the 32-bit integers in the |
| 359 |
output string in the native byte order of the machine where the Tcl |
| 360 |
script is running. |
| 361 |
To determine what the native byte order of the machine is, refer to |
| 362 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 363 |
.IP \fBw\fR 5 |
| 364 |
This form is the same as \fBc\fR except that it stores one or more |
| 365 |
64-bit integers in little-endian byte order in the output string. The |
| 366 |
low-order 64-bits of each integer are stored as an eight-byte value at |
| 367 |
the cursor position with the least significant byte stored first. For |
| 368 |
example, |
| 369 |
.RS |
| 370 |
.CS |
| 371 |
\fBbinary format\fR w 7810179016327718216 |
| 372 |
.CE |
| 373 |
will return the string \fBHelloTcl\fR |
| 374 |
.RE |
| 375 |
.IP \fBW\fR 5 |
| 376 |
This form is the same as \fBw\fR except that it stores one or more one |
| 377 |
or more 64-bit integers in big-endian byte order in the output string. |
| 378 |
For example, |
| 379 |
.RS |
| 380 |
.CS |
| 381 |
\fBbinary format\fR Wc 4785469626960341345 110 |
| 382 |
.CE |
| 383 |
will return the string \fBBigEndian\fR |
| 384 |
.RE |
| 385 |
.IP \fBm\fR 5 |
| 386 |
This form (mnemonically the mirror of \fBw\fR) is the same as \fBw\fR |
| 387 |
and \fBW\fR except that it stores the 64-bit integers in the output |
| 388 |
string in the native byte order of the machine where the Tcl script is |
| 389 |
running. |
| 390 |
To determine what the native byte order of the machine is, refer to |
| 391 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 392 |
.IP \fBf\fR 5 |
| 393 |
This form is the same as \fBc\fR except that it stores one or more one |
| 394 |
or more single-precision floating point numbers in the machine's native |
| 395 |
representation in the output string. This representation is not |
| 396 |
portable across architectures, so it should not be used to communicate |
| 397 |
floating point numbers across the network. The size of a floating |
| 398 |
point number may vary across architectures, so the number of bytes |
| 399 |
that are generated may vary. If the value overflows the |
| 400 |
machine's native representation, then the value of FLT_MAX |
| 401 |
as defined by the system will be used instead. Because Tcl uses |
| 402 |
double-precision floating point numbers internally, there may be some |
| 403 |
loss of precision in the conversion to single-precision. For example, |
| 404 |
on a Windows system running on an Intel Pentium processor, |
| 405 |
.RS |
| 406 |
.CS |
| 407 |
\fBbinary format\fR f2 {1.6 3.4} |
| 408 |
.CE |
| 409 |
will return a string equivalent to |
| 410 |
\fB\excd\excc\excc\ex3f\ex9a\ex99\ex59\ex40\fR. |
| 411 |
.RE |
| 412 |
.IP \fBr\fR 5 |
| 413 |
This form (mnemonically \fIreal\fR) is the same as \fBf\fR except that |
| 414 |
it stores the single-precision floating point numbers in little-endian |
| 415 |
order. This conversion only produces meaningful output when used on |
| 416 |
machines which use the IEEE floating point representation (very |
| 417 |
common, but not universal.) |
| 418 |
.IP \fBR\fR 5 |
| 419 |
This form is the same as \fBr\fR except that it stores the |
| 420 |
single-precision floating point numbers in big-endian order. |
| 421 |
.IP \fBd\fR 5 |
| 422 |
This form is the same as \fBf\fR except that it stores one or more one |
| 423 |
or more double-precision floating point numbers in the machine's native |
| 424 |
representation in the output string. For example, on a |
| 425 |
Windows system running on an Intel Pentium processor, |
| 426 |
.RS |
| 427 |
.CS |
| 428 |
\fBbinary format\fR d1 {1.6} |
| 429 |
.CE |
| 430 |
will return a string equivalent to |
| 431 |
\fB\ex9a\ex99\ex99\ex99\ex99\ex99\exf9\ex3f\fR. |
| 432 |
.RE |
| 433 |
.IP \fBq\fR 5 |
| 434 |
This form (mnemonically the mirror of \fBd\fR) is the same as \fBd\fR |
| 435 |
except that it stores the double-precision floating point numbers in |
| 436 |
little-endian order. This conversion only produces meaningful output |
| 437 |
when used on machines which use the IEEE floating point representation |
| 438 |
(very common, but not universal.) |
| 439 |
.IP \fBQ\fR 5 |
| 440 |
This form is the same as \fBq\fR except that it stores the |
| 441 |
double-precision floating point numbers in big-endian order. |
| 442 |
.IP \fBx\fR 5 |
| 443 |
Stores \fIcount\fR null bytes in the output string. If \fIcount\fR is |
| 444 |
not specified, stores one null byte. If \fIcount\fR is \fB*\fR, |
| 445 |
generates an error. This type does not consume an argument. For |
| 446 |
example, |
| 447 |
.RS |
| 448 |
.CS |
| 449 |
\fBbinary format\fR a3xa3x2a3 abc def ghi |
| 450 |
.CE |
| 451 |
will return a string equivalent to \fBabc\e000def\e000\e000ghi\fR. |
| 452 |
.RE |
| 453 |
.IP \fBX\fR 5 |
| 454 |
Moves the cursor back \fIcount\fR bytes in the output string. If |
| 455 |
\fIcount\fR is \fB*\fR or is larger than the current cursor position, |
| 456 |
then the cursor is positioned at location 0 so that the next byte |
| 457 |
stored will be the first byte in the result string. If \fIcount\fR is |
| 458 |
omitted then the cursor is moved back one byte. This type does not |
| 459 |
consume an argument. For example, |
| 460 |
.RS |
| 461 |
.CS |
| 462 |
\fBbinary format\fR a3X*a3X2a3 abc def ghi |
| 463 |
.CE |
| 464 |
will return \fBdghi\fR. |
| 465 |
.RE |
| 466 |
.IP \fB@\fR 5 |
| 467 |
Moves the cursor to the absolute location in the output string |
| 468 |
specified by \fIcount\fR. Position 0 refers to the first byte in the |
| 469 |
output string. If \fIcount\fR refers to a position beyond the last |
| 470 |
byte stored so far, then null bytes will be placed in the uninitialized |
| 471 |
locations and the cursor will be placed at the specified location. If |
| 472 |
\fIcount\fR is \fB*\fR, then the cursor is moved to the current end of |
| 473 |
the output string. If \fIcount\fR is omitted, then an error will be |
| 474 |
generated. This type does not consume an argument. For example, |
| 475 |
.RS |
| 476 |
.CS |
| 477 |
\fBbinary format\fR a5@2a1@*a3@10a1 abcde f ghi j |
| 478 |
.CE |
| 479 |
will return \fBabfdeghi\e000\e000j\fR. |
| 480 |
.RE |
| 481 |
.SH "BINARY SCAN" |
| 482 |
.PP |
| 483 |
The \fBbinary scan\fR command parses fields from a binary string, |
| 484 |
returning the number of conversions performed. \fIString\fR gives the |
| 485 |
input bytes to be parsed (one byte per character, and characters not |
| 486 |
representable as a byte have their high bits chopped) |
| 487 |
and \fIformatString\fR indicates how to parse it. |
| 488 |
Each \fIvarName\fR gives the name of a variable; when a field is |
| 489 |
scanned from \fIstring\fR the result is assigned to the corresponding |
| 490 |
variable. |
| 491 |
.PP |
| 492 |
As with \fBbinary format\fR, the \fIformatString\fR consists of a |
| 493 |
sequence of zero or more field specifiers separated by zero or more |
| 494 |
spaces. Each field specifier is a single type character followed by |
| 495 |
an optional flag character followed by an optional numeric \fIcount\fR. |
| 496 |
Most field specifiers consume one |
| 497 |
argument to obtain the variable into which the scanned values should |
| 498 |
be placed. The type character specifies how the binary data is to be |
| 499 |
interpreted. The \fIcount\fR typically indicates how many items of |
| 500 |
the specified type are taken from the data. If present, the |
| 501 |
\fIcount\fR is a non-negative decimal integer or \fB*\fR, which |
| 502 |
normally indicates that all of the remaining items in the data are to |
| 503 |
be used. If there are not enough bytes left after the current cursor |
| 504 |
position to satisfy the current field specifier, then the |
| 505 |
corresponding variable is left untouched and \fBbinary scan\fR returns |
| 506 |
immediately with the number of variables that were set. If there are |
| 507 |
not enough arguments for all of the fields in the format string that |
| 508 |
consume arguments, then an error is generated. The flag character |
| 509 |
.QW u |
| 510 |
may be given to cause some types to be read as unsigned values. The flag |
| 511 |
is accepted for all field types but is ignored for non-integer fields. |
| 512 |
.PP |
| 513 |
A similar example as with \fBbinary format\fR should explain the |
| 514 |
relation between field specifiers and arguments in case of the binary |
| 515 |
scan subcommand: |
| 516 |
.CS |
| 517 |
\fBbinary scan\fR $bytes s3s first second |
| 518 |
.CE |
| 519 |
.PP |
| 520 |
This command (provided the binary string in the variable \fIbytes\fR |
| 521 |
is long enough) assigns a list of three integers to the variable |
| 522 |
\fIfirst\fR and assigns a single value to the variable \fIsecond\fR. |
| 523 |
If \fIbytes\fR contains fewer than 8 bytes (i.e. four 2-byte |
| 524 |
integers), no assignment to \fIsecond\fR will be made, and if |
| 525 |
\fIbytes\fR contains fewer than 6 bytes (i.e. three 2-byte integers), |
| 526 |
no assignment to \fIfirst\fR will be made. Hence: |
| 527 |
.CS |
| 528 |
puts [\fBbinary scan\fR abcdefg s3s first second] |
| 529 |
puts $first |
| 530 |
puts $second |
| 531 |
.CE |
| 532 |
will print (assuming neither variable is set previously): |
| 533 |
.CS |
| 534 |
1 |
| 535 |
25185 25699 26213 |
| 536 |
can't read "second": no such variable |
| 537 |
.CE |
| 538 |
.PP |
| 539 |
It is \fIimportant\fR to note that the \fBc\fR, \fBs\fR, and \fBS\fR |
| 540 |
(and \fBi\fR and \fBI\fR on 64bit systems) will be scanned into |
| 541 |
long data size values. In doing this, values that have their high |
| 542 |
bit set (0x80 for chars, 0x8000 for shorts, 0x80000000 for ints), |
| 543 |
will be sign extended. Thus the following will occur: |
| 544 |
.CS |
| 545 |
set signShort [\fBbinary format\fR s1 0x8000] |
| 546 |
\fBbinary scan\fR $signShort s1 val; \fI# val == 0xFFFF8000\fR |
| 547 |
.CE |
| 548 |
If you require unsigned values you can include the |
| 549 |
.QW u |
| 550 |
flag character following |
| 551 |
the field type. For example, to read an unsigned short value: |
| 552 |
.CS |
| 553 |
set signShort [\fBbinary format\fR s1 0x8000] |
| 554 |
\fBbinary scan\fR $signShort su1 val; \fI# val == 0x00008000\fR |
| 555 |
.CE |
| 556 |
.PP |
| 557 |
Each type-count pair moves an imaginary cursor through the binary data, |
| 558 |
reading bytes from the current position. The cursor is initially |
| 559 |
at position 0 at the beginning of the data. The type may be any one of |
| 560 |
the following characters: |
| 561 |
.IP \fBa\fR 5 |
| 562 |
The data is a byte string of length \fIcount\fR. If \fIcount\fR |
| 563 |
is \fB*\fR, then all of the remaining bytes in \fIstring\fR will be |
| 564 |
scanned into the variable. If \fIcount\fR is omitted, then one |
| 565 |
byte will be scanned. |
| 566 |
All bytes scanned will be interpreted as being characters in the |
| 567 |
range \eu0000-\eu00ff so the \fBencoding convertfrom\fR command will be |
| 568 |
needed if the string is not a binary string or a string encoded in ISO |
| 569 |
8859\-1. |
| 570 |
For example, |
| 571 |
.RS |
| 572 |
.CS |
| 573 |
\fBbinary scan\fR abcde\e000fghi a6a10 var1 var2 |
| 574 |
.CE |
| 575 |
will return \fB1\fR with the string equivalent to \fBabcde\e000\fR |
| 576 |
stored in \fIvar1\fR and \fIvar2\fR left unmodified, and |
| 577 |
.CS |
| 578 |
\fBbinary scan\fR \e342\e202\e254 a* var1 |
| 579 |
set var2 [encoding convertfrom utf-8 $var1] |
| 580 |
.CE |
| 581 |
will store a Euro-currency character in \fIvar2\fR. |
| 582 |
.RE |
| 583 |
.IP \fBA\fR 5 |
| 584 |
This form is the same as \fBa\fR, except trailing blanks and nulls are stripped from |
| 585 |
the scanned value before it is stored in the variable. For example, |
| 586 |
.RS |
| 587 |
.CS |
| 588 |
\fBbinary scan\fR "abc efghi \e000" A* var1 |
| 589 |
.CE |
| 590 |
will return \fB1\fR with \fBabc efghi\fR stored in \fIvar1\fR. |
| 591 |
.RE |
| 592 |
.IP \fBb\fR 5 |
| 593 |
The data is turned into a string of \fIcount\fR binary digits in |
| 594 |
low-to-high order represented as a sequence of |
| 595 |
.QW 1 |
| 596 |
and |
| 597 |
.QW 0 |
| 598 |
characters. The data bytes are scanned in first to last order with |
| 599 |
the bits being taken in low-to-high order within each byte. Any extra |
| 600 |
bits in the last byte are ignored. If \fIcount\fR is \fB*\fR, then |
| 601 |
all of the remaining bits in \fIstring\fR will be scanned. If |
| 602 |
\fIcount\fR is omitted, then one bit will be scanned. For example, |
| 603 |
.RS |
| 604 |
.CS |
| 605 |
\fBbinary scan\fR \ex07\ex87\ex05 b5b* var1 var2 |
| 606 |
.CE |
| 607 |
will return \fB2\fR with \fB11100\fR stored in \fIvar1\fR and |
| 608 |
\fB1110000110100000\fR stored in \fIvar2\fR. |
| 609 |
.RE |
| 610 |
.IP \fBB\fR 5 |
| 611 |
This form is the same as \fBb\fR, except the bits are taken in |
| 612 |
high-to-low order within each byte. For example, |
| 613 |
.RS |
| 614 |
.CS |
| 615 |
\fBbinary scan\fR \ex70\ex87\ex05 B5B* var1 var2 |
| 616 |
.CE |
| 617 |
will return \fB2\fR with \fB01110\fR stored in \fIvar1\fR and |
| 618 |
\fB1000011100000101\fR stored in \fIvar2\fR. |
| 619 |
.RE |
| 620 |
.IP \fBH\fR 5 |
| 621 |
The data is turned into a string of \fIcount\fR hexadecimal digits in |
| 622 |
high-to-low order represented as a sequence of characters in the set |
| 623 |
.QW 0123456789abcdef . |
| 624 |
The data bytes are scanned in first to last |
| 625 |
order with the hex digits being taken in high-to-low order within each |
| 626 |
byte. Any extra bits in the last byte are ignored. If \fIcount\fR is |
| 627 |
\fB*\fR, then all of the remaining hex digits in \fIstring\fR will be |
| 628 |
scanned. If \fIcount\fR is omitted, then one hex digit will be |
| 629 |
scanned. For example, |
| 630 |
.RS |
| 631 |
.CS |
| 632 |
\fBbinary scan\fR \ex07\exC6\ex05\ex1f\ex34 H3H* var1 var2 |
| 633 |
.CE |
| 634 |
will return \fB2\fR with \fB07c\fR stored in \fIvar1\fR and |
| 635 |
\fB051f34\fR stored in \fIvar2\fR. |
| 636 |
.RE |
| 637 |
.IP \fBh\fR 5 |
| 638 |
This form is the same as \fBH\fR, except the digits are taken in |
| 639 |
reverse (low-to-high) order within each byte. For example, |
| 640 |
.RS |
| 641 |
.CS |
| 642 |
\fBbinary scan\fR \ex07\ex86\ex05\ex12\ex34 h3h* var1 var2 |
| 643 |
.CE |
| 644 |
will return \fB2\fR with \fB706\fR stored in \fIvar1\fR and |
| 645 |
\fB502143\fR stored in \fIvar2\fR. |
| 646 |
.PP |
| 647 |
Note that most code that wishes to parse the hexadecimal digits from |
| 648 |
multiple bytes in order should use the \fBH\fR format. |
| 649 |
.RE |
| 650 |
.IP \fBc\fR 5 |
| 651 |
The data is turned into \fIcount\fR 8-bit signed integers and stored |
| 652 |
in the corresponding variable as a list. If \fIcount\fR is \fB*\fR, |
| 653 |
then all of the remaining bytes in \fIstring\fR will be scanned. If |
| 654 |
\fIcount\fR is omitted, then one 8-bit integer will be scanned. For |
| 655 |
example, |
| 656 |
.RS |
| 657 |
.CS |
| 658 |
\fBbinary scan\fR \ex07\ex86\ex05 c2c* var1 var2 |
| 659 |
.CE |
| 660 |
will return \fB2\fR with \fB7 -122\fR stored in \fIvar1\fR and \fB5\fR |
| 661 |
stored in \fIvar2\fR. Note that the integers returned are signed, but |
| 662 |
they can be converted to unsigned 8-bit quantities using an expression |
| 663 |
like: |
| 664 |
.CS |
| 665 |
set num [expr { $num & 0xFF }] |
| 666 |
.CE |
| 667 |
.RE |
| 668 |
.IP \fBs\fR 5 |
| 669 |
The data is interpreted as \fIcount\fR 16-bit signed integers |
| 670 |
represented in little-endian byte order. The integers are stored in |
| 671 |
the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then |
| 672 |
all of the remaining bytes in \fIstring\fR will be scanned. If |
| 673 |
\fIcount\fR is omitted, then one 16-bit integer will be scanned. For |
| 674 |
example, |
| 675 |
.RS |
| 676 |
.CS |
| 677 |
\fBbinary scan\fR \ex05\ex00\ex07\ex00\exf0\exff s2s* var1 var2 |
| 678 |
.CE |
| 679 |
will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR |
| 680 |
stored in \fIvar2\fR. Note that the integers returned are signed, but |
| 681 |
they can be converted to unsigned 16-bit quantities using an expression |
| 682 |
like: |
| 683 |
.CS |
| 684 |
set num [expr { $num & 0xFFFF }] |
| 685 |
.CE |
| 686 |
.RE |
| 687 |
.IP \fBS\fR 5 |
| 688 |
This form is the same as \fBs\fR except that the data is interpreted |
| 689 |
as \fIcount\fR 16-bit signed integers represented in big-endian byte |
| 690 |
order. For example, |
| 691 |
.RS |
| 692 |
.CS |
| 693 |
\fBbinary scan\fR \ex00\ex05\ex00\ex07\exff\exf0 S2S* var1 var2 |
| 694 |
.CE |
| 695 |
will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR |
| 696 |
stored in \fIvar2\fR. |
| 697 |
.RE |
| 698 |
.IP \fBt\fR 5 |
| 699 |
The data is interpreted as \fIcount\fR 16-bit signed integers |
| 700 |
represented in the native byte order of the machine running the Tcl |
| 701 |
script. It is otherwise identical to \fBs\fR and \fBS\fR. |
| 702 |
To determine what the native byte order of the machine is, refer to |
| 703 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 704 |
.IP \fBi\fR 5 |
| 705 |
The data is interpreted as \fIcount\fR 32-bit signed integers |
| 706 |
represented in little-endian byte order. The integers are stored in |
| 707 |
the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then |
| 708 |
all of the remaining bytes in \fIstring\fR will be scanned. If |
| 709 |
\fIcount\fR is omitted, then one 32-bit integer will be scanned. For |
| 710 |
example, |
| 711 |
.RS |
| 712 |
.CS |
| 713 |
set str \ex05\ex00\ex00\ex00\ex07\ex00\ex00\ex00\exf0\exff\exff\exff |
| 714 |
\fBbinary scan\fR $str i2i* var1 var2 |
| 715 |
.CE |
| 716 |
will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR |
| 717 |
stored in \fIvar2\fR. Note that the integers returned are signed, but |
| 718 |
they can be converted to unsigned 32-bit quantities using an expression |
| 719 |
like: |
| 720 |
.CS |
| 721 |
set num [expr { $num & 0xFFFFFFFF }] |
| 722 |
.CE |
| 723 |
.RE |
| 724 |
.IP \fBI\fR 5 |
| 725 |
This form is the same as \fBI\fR except that the data is interpreted |
| 726 |
as \fIcount\fR 32-bit signed integers represented in big-endian byte |
| 727 |
order. For example, |
| 728 |
.RS |
| 729 |
.CS |
| 730 |
set str \ex00\ex00\ex00\ex05\ex00\ex00\ex00\ex07\exff\exff\exff\exf0 |
| 731 |
\fBbinary scan\fR $str I2I* var1 var2 |
| 732 |
.CE |
| 733 |
will return \fB2\fR with \fB5 7\fR stored in \fIvar1\fR and \fB\-16\fR |
| 734 |
stored in \fIvar2\fR. |
| 735 |
.RE |
| 736 |
.IP \fBn\fR 5 |
| 737 |
The data is interpreted as \fIcount\fR 32-bit signed integers |
| 738 |
represented in the native byte order of the machine running the Tcl |
| 739 |
script. It is otherwise identical to \fBi\fR and \fBI\fR. |
| 740 |
To determine what the native byte order of the machine is, refer to |
| 741 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 742 |
.IP \fBw\fR 5 |
| 743 |
The data is interpreted as \fIcount\fR 64-bit signed integers |
| 744 |
represented in little-endian byte order. The integers are stored in |
| 745 |
the corresponding variable as a list. If \fIcount\fR is \fB*\fR, then |
| 746 |
all of the remaining bytes in \fIstring\fR will be scanned. If |
| 747 |
\fIcount\fR is omitted, then one 64-bit integer will be scanned. For |
| 748 |
example, |
| 749 |
.RS |
| 750 |
.CS |
| 751 |
set str \ex05\ex00\ex00\ex00\ex07\ex00\ex00\ex00\exf0\exff\exff\exff |
| 752 |
\fBbinary scan\fR $str wi* var1 var2 |
| 753 |
.CE |
| 754 |
will return \fB2\fR with \fB30064771077\fR stored in \fIvar1\fR and |
| 755 |
\fB\-16\fR stored in \fIvar2\fR. Note that the integers returned are |
| 756 |
signed and cannot be represented by Tcl as unsigned values. |
| 757 |
.RE |
| 758 |
.IP \fBW\fR 5 |
| 759 |
This form is the same as \fBw\fR except that the data is interpreted |
| 760 |
as \fIcount\fR 64-bit signed integers represented in big-endian byte |
| 761 |
order. For example, |
| 762 |
.RS |
| 763 |
.CS |
| 764 |
set str \ex00\ex00\ex00\ex05\ex00\ex00\ex00\ex07\exff\exff\exff\exf0 |
| 765 |
\fBbinary scan\fR $str WI* var1 var2 |
| 766 |
.CE |
| 767 |
will return \fB2\fR with \fB21474836487\fR stored in \fIvar1\fR and \fB\-16\fR |
| 768 |
stored in \fIvar2\fR. |
| 769 |
.RE |
| 770 |
.IP \fBm\fR 5 |
| 771 |
The data is interpreted as \fIcount\fR 64-bit signed integers |
| 772 |
represented in the native byte order of the machine running the Tcl |
| 773 |
script. It is otherwise identical to \fBw\fR and \fBW\fR. |
| 774 |
To determine what the native byte order of the machine is, refer to |
| 775 |
the \fBbyteOrder\fR element of the \fBtcl_platform\fR array. |
| 776 |
.IP \fBf\fR 5 |
| 777 |
The data is interpreted as \fIcount\fR single-precision floating point |
| 778 |
numbers in the machine's native representation. The floating point |
| 779 |
numbers are stored in the corresponding variable as a list. If |
| 780 |
\fIcount\fR is \fB*\fR, then all of the remaining bytes in |
| 781 |
\fIstring\fR will be scanned. If \fIcount\fR is omitted, then one |
| 782 |
single-precision floating point number will be scanned. The size of a |
| 783 |
floating point number may vary across architectures, so the number of |
| 784 |
bytes that are scanned may vary. If the data does not represent a |
| 785 |
valid floating point number, the resulting value is undefined and |
| 786 |
compiler dependent. For example, on a Windows system running on an |
| 787 |
Intel Pentium processor, |
| 788 |
.RS |
| 789 |
.CS |
| 790 |
\fBbinary scan\fR \ex3f\excc\excc\excd f var1 |
| 791 |
.CE |
| 792 |
will return \fB1\fR with \fB1.6000000238418579\fR stored in |
| 793 |
\fIvar1\fR. |
| 794 |
.RE |
| 795 |
.IP \fBr\fR 5 |
| 796 |
This form is the same as \fBf\fR except that the data is interpreted |
| 797 |
as \fIcount\fR single-precision floating point number in little-endian |
| 798 |
order. This conversion is not portable to the minority of systems not |
| 799 |
using IEEE floating point representations. |
| 800 |
.IP \fBR\fR 5 |
| 801 |
This form is the same as \fBf\fR except that the data is interpreted |
| 802 |
as \fIcount\fR single-precision floating point number in big-endian |
| 803 |
order. This conversion is not portable to the minority of systems not |
| 804 |
using IEEE floating point representations. |
| 805 |
.IP \fBd\fR 5 |
| 806 |
This form is the same as \fBf\fR except that the data is interpreted |
| 807 |
as \fIcount\fR double-precision floating point numbers in the |
| 808 |
machine's native representation. For example, on a Windows system |
| 809 |
running on an Intel Pentium processor, |
| 810 |
.RS |
| 811 |
.CS |
| 812 |
\fBbinary scan\fR \ex9a\ex99\ex99\ex99\ex99\ex99\exf9\ex3f d var1 |
| 813 |
.CE |
| 814 |
will return \fB1\fR with \fB1.6000000000000001\fR |
| 815 |
stored in \fIvar1\fR. |
| 816 |
.RE |
| 817 |
.IP \fBq\fR 5 |
| 818 |
This form is the same as \fBd\fR except that the data is interpreted |
| 819 |
as \fIcount\fR double-precision floating point number in little-endian |
| 820 |
order. This conversion is not portable to the minority of systems not |
| 821 |
using IEEE floating point representations. |
| 822 |
.IP \fBQ\fR 5 |
| 823 |
This form is the same as \fBd\fR except that the data is interpreted |
| 824 |
as \fIcount\fR double-precision floating point number in big-endian |
| 825 |
order. This conversion is not portable to the minority of systems not |
| 826 |
using IEEE floating point representations. |
| 827 |
.IP \fBx\fR 5 |
| 828 |
Moves the cursor forward \fIcount\fR bytes in \fIstring\fR. If |
| 829 |
\fIcount\fR is \fB*\fR or is larger than the number of bytes after the |
| 830 |
current cursor position, then the cursor is positioned after |
| 831 |
the last byte in \fIstring\fR. If \fIcount\fR is omitted, then the |
| 832 |
cursor is moved forward one byte. Note that this type does not |
| 833 |
consume an argument. For example, |
| 834 |
.RS |
| 835 |
.CS |
| 836 |
\fBbinary scan\fR \ex01\ex02\ex03\ex04 x2H* var1 |
| 837 |
.CE |
| 838 |
will return \fB1\fR with \fB0304\fR stored in \fIvar1\fR. |
| 839 |
.RE |
| 840 |
.IP \fBX\fR 5 |
| 841 |
Moves the cursor back \fIcount\fR bytes in \fIstring\fR. If |
| 842 |
\fIcount\fR is \fB*\fR or is larger than the current cursor position, |
| 843 |
then the cursor is positioned at location 0 so that the next byte |
| 844 |
scanned will be the first byte in \fIstring\fR. If \fIcount\fR |
| 845 |
is omitted then the cursor is moved back one byte. Note that this |
| 846 |
type does not consume an argument. For example, |
| 847 |
.RS |
| 848 |
.CS |
| 849 |
\fBbinary scan\fR \ex01\ex02\ex03\ex04 c2XH* var1 var2 |
| 850 |
.CE |
| 851 |
will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR |
| 852 |
stored in \fIvar2\fR. |
| 853 |
.RE |
| 854 |
.IP \fB@\fR 5 |
| 855 |
Moves the cursor to the absolute location in the data string specified |
| 856 |
by \fIcount\fR. Note that position 0 refers to the first byte in |
| 857 |
\fIstring\fR. If \fIcount\fR refers to a position beyond the end of |
| 858 |
\fIstring\fR, then the cursor is positioned after the last byte. If |
| 859 |
\fIcount\fR is omitted, then an error will be generated. For example, |
| 860 |
.RS |
| 861 |
.CS |
| 862 |
\fBbinary scan\fR \ex01\ex02\ex03\ex04 c2@1H* var1 var2 |
| 863 |
.CE |
| 864 |
will return \fB2\fR with \fB1 2\fR stored in \fIvar1\fR and \fB020304\fR |
| 865 |
stored in \fIvar2\fR. |
| 866 |
.RE |
| 867 |
.SH "PORTABILITY ISSUES" |
| 868 |
.PP |
| 869 |
The \fBr\fR, \fBR\fR, \fBq\fR and \fBQ\fR conversions will only work |
| 870 |
reliably for transferring data between computers which are all using |
| 871 |
IEEE floating point representations. This is very common, but not |
| 872 |
universal. To transfer floating-point numbers portably between all |
| 873 |
architectures, use their textual representation (as produced by |
| 874 |
\fBformat\fR) instead. |
| 875 |
.SH EXAMPLES |
| 876 |
.PP |
| 877 |
This is a procedure to write a Tcl string to a binary-encoded channel as |
| 878 |
UTF-8 data preceded by a length word: |
| 879 |
.PP |
| 880 |
.CS |
| 881 |
proc \fIwriteString\fR {channel string} { |
| 882 |
set data [encoding convertto utf-8 $string] |
| 883 |
puts -nonewline [\fBbinary format\fR Ia* \e |
| 884 |
[string length $data] $data] |
| 885 |
} |
| 886 |
.CE |
| 887 |
.PP |
| 888 |
This procedure reads a string from a channel that was written by the |
| 889 |
previously presented \fIwriteString\fR procedure: |
| 890 |
.PP |
| 891 |
.CS |
| 892 |
proc \fIreadString\fR {channel} { |
| 893 |
if {![\fBbinary scan\fR [read $channel 4] I length]} { |
| 894 |
error "missing length" |
| 895 |
} |
| 896 |
set data [read $channel $length] |
| 897 |
return [encoding convertfrom utf-8 $data] |
| 898 |
} |
| 899 |
.CE |
| 900 |
.PP |
| 901 |
This converts the contents of a file (named in the variable \fIfilename\fR) to |
| 902 |
base64 and prints them: |
| 903 |
.PP |
| 904 |
.CS |
| 905 |
set f [open $filename rb] |
| 906 |
set data [read $f] |
| 907 |
close $f |
| 908 |
puts [\fBbinary encode\fR base64 \-maxlen 64 $data] |
| 909 |
.CE |
| 910 |
.SH "SEE ALSO" |
| 911 |
encoding(n), format(n), scan(n), string(n), tcl_platform(n) |
| 912 |
.SH KEYWORDS |
| 913 |
binary, format, scan |
| 914 |
'\" Local Variables: |
| 915 |
'\" mode: nroff |
| 916 |
'\" fill-column: 78 |
| 917 |
'\" End: |