How to combine two numbers to one and vice versa
To combine two numbers into one, use the next function
nCombined = VisNumberMakeLong( nValue1, nValue2 )
You can find this function in vtmisc.apl
Beware that the two input parameters must have a value between 0 and 65535.
To get the two values back from the combined number
Set nValue1= SalNumberLow( nCombined ) Set nValue2= SalNumberHigh( nCombined )
How to convert a number to string using prefixed zeros
An easy way to do that is using this function
sResult = SalFmtFormatNumber( nValue, sPicture )
So if you want to have a string with 5 characters and if nValue results in less characters and should be prefixed with zeros
Set nValue = 12 Set sResult = SalFmtFormatNumber( nValue, "00000" ) ! Picture parameter has 5 zeros specified ! sResult has the value "00012"
An alternative way to define a number in boolean expressions
Look at the next piece of code
If bOk = TRUE Set nValue = 1 Else Set nValue = -1
It can be rewritten using one line of code using this function from vtmisc.apl
nResult = VisNumberChoose( bExpression, nTrueNumber, nFalseNumber )
So the If/Else construction above can be rewritten to
Set nValue = VisNumberChoose( bOk = TRUE, 1, -1 )
How to convert from one base to another (eg decimal to hexadecimal to binary)
Developers familiar with the C(++) language will probably know conversion functions like itoa and strtol.
The itoa function (integer to ASCII) is able to convert/format a decimal integer number to a given base (eg hexadecimal, binary, octal etc).
The function strtol does the opposite, converting a base to decimal.
These functions are also available to other programming languages with the MSCRT library (C runtime).
So also for TD.
First declare these external functions (below the declarations for TD versions prior to TD5.1, ANSI) :
Library name: MSVCRT.dll Function: _itoa Export Ordinal: 0 Returns Parameters Number: INT Receive String: LPSTR Number: INT Function: strtol Export Ordinal: 0 Returns Number: LONG Parameters String: LPCSTR Number: LPVOID Number: INT
Below the declarations for TD 5.1 and higher versions (UNICODE) :
Library name: MSVCRT.dll Function: _itow Export Ordinal: 0 Returns String: LPWSTR Parameters Number: INT Receive String: LPWSTR Number: INT Function: wcstol Export Ordinal: 0 Returns Number: LONG Parameters String: LPCWSTR Number: LPVOID Number: INT
See more info concerning these CRT functions on MSDN:
The next piece of code converts a decimal integer to the given base
Call SalStrSetBufferLength( sBuffer, 32 ) ! Set nToBase to 2..36 (2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal) Call _itoa( nDecimalNumber, sBuffer, nToBase ) ! sBuffer holds the converted decimal in the given base
Here the code to convert a base number to decimal
Set nNumber = strtol( sBaseValue, NUMBER_Null, nBase )
! Next, decimal to hexadecimal Call _itoa( 125, sBuffer, 16 ) ! sBuffer = "7d" ! Next, decimal to octal Call _itoa( 125, sBuffer, 8 ) ! sBuffer = "175" ! Next, decimal to binary Call _itoa( 125, sBuffer, 2) ! sBuffer = "1111101" ! ! Next, binary to decimal Set nNumber = strtol( "101, NUMBER_Null, 2 ) ! nNumber = 5
Using these functions you are able to convert from one base to another.
The sample can be downloaded below, it uses wrapper PAL functions for the CRT functions and a Base2Base function.
(Separate samples for TD5.1 and higher also provided).
Endianness in TD: Little-endian vs Big-endian
In computing, memory commonly stores binary data by splitting it into 8 bit units (bytes).
When a data word uses multiple such units, the order of storing these bytes into memory becomes important.
The terms endian and endianness, refer to how bytes of a data word are ordered within memory.
Windows (and TD) is using Little-endian to store the sequence of bytes in memory.
Let's compare the two endianesses:
Assume we have a 32 bit number (= 4 bytes) value represented as hex value : 0xAABBCCDD
A TD number value will be stored as Little-endian which look like this :
DD CC BB AA
The same value in Big-endian looks like this:
AA BB CC DD
The difference is the sequence from left to right where the bytes are stored.
When you need to use Big-endian numbers, for instance when sending data to an external system which uses Big-endian sequence
you need to convert the TD Little-endian buffer to Big-endian before sending it.
When you send the Little-endian style, the receiving party will interpret the data incorrectly.
And also the way back. receiving Big-endian data will be wrongly interpreted by TD. You first have to convert the value
to Little-endian before using it.
To convert a TD number to Big-endian and back to Little-endian, use the WinApi functions in the WinSock library.
With the functions htons (16 bits value), htonl (32 bits value) you convert Little-endian to Big-endian.
With the functions ntohs (16 bits value), ntohl (32 bits value) you convert Big-endian to Little-endian.
Look at the sample application which shows how to implement this.