SWAP() Function - Swap Adjacent Bytes
Syntax
SWAP(str1{,str2}{,ERR=lineref})
Description
The SWAP() function is used for exchanging data with other software applications that may be using a native mode of data representation on a particular machine. It rearranges the bytes in str1 according to specific rules and returns the string result. The rules are provided by individual bits in the optional str2, or by default if str2 is not given. str2 must be one byte long. The lower three bits determine the kind of swapping to take place.
$01$ |
Swap adjacent bytes. |
$02$ |
Swap adjacent words. |
$04$ |
Swap adjacent long words. |
Any combination of these bits is legal. The table below illustrates the results of SWAP() functions with the various combinations:
|
Long |
Long |
||||||
Swap |
Word |
Word |
Word |
Word |
||||
Option |
Byte |
Byte |
Byte |
Byte |
Byte |
Byte |
Byte |
Byte |
$00$ |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
$01$ |
1 |
0 |
3 |
2 |
5 |
4 |
7 |
6 |
$02$ |
2 |
3 |
0 |
1 |
6 |
7 |
4 |
5 |
$03$ |
3 |
2 |
1 |
0 |
7 |
6 |
5 |
4 |
$04$ |
4 |
5 |
6 |
7 |
0 |
1 |
2 |
3 |
$05$ |
5 |
4 |
7 |
6 |
1 |
0 |
3 |
2 |
$06$ |
6 |
7 |
4 |
5 |
2 |
3 |
0 |
1 |
$07$ |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
If the length of str1 is not
a useful multiple of 2, 4, or 8, the last few characters of the string
may be left alone. If the str2
argument is not given, PRO/5 swaps bytes according to the architecture
of the host computer it is running on.
PRO/5 always treats binary data in a natural left-to-right manner, as shown
by the DEC(), BIN(), FDEC(), and FBIN() functions and provides the high
degree of compatibility of PRO/5 implementations. Some CPUs work backwards.
If a program just read-in an 8-byte IEEE value from a spreadsheet, it
may not be ready for the FDEC() function. This can be corrected with the
SWAP() function:
>LET A=FDEC(SWAP(A$))
Likewise, writing binary data to another application may require:
>PRINT (1)SWAP(FBIN(A,4)),
When interacting with data created by other software, check the data
and file format documentation to determine the byte ordering method used
by that software.
Printing the value of SWAP("76543210") displays the SWAP order
for the current system.