Tech Note 10: The Bits 'n Bytes LibJanuary 14, 2009© NSB Corporation. All rights reserved. |
The BitsNbyteslib adds a number of handy functions to NS Basic for doing bit manipulation and conversions. You can do a variety of bitwise AND, OR and NOT operations, shift bits left and right, and convert between hex, binary and decimal.
To use it, add BitsnBytesLib.prc to your project as a resource. It is a small file, about 7k in size. Initialise it in your startup code as follows:
loadLibrary "BitsNBytesLib","BNB" 'case is important
These functions require datatypes to be exactly as defined. Most of the arguments are required to be integers. Integers for these calls are 32 bits. Shorts are 16 bits. They are the same as standard NS Basic integer and short variables. If you use incorrect datatypes, you will get execution errors at runtime.
When calling routines in libraries, it is important to use the correct variable types. The calling routine uses the variable type to properly place the values on the stack to be passed to the library. A call such as
c=BNB.xorInteger(16,4)will not work because the library calling routine cannot determine whether 16 and 4 should be short, integer or float datatypes for the call. The call will work properly if you assign 16 and 4 to variables that you use in the function statement:
dim a as integer dim b as integer a=16 b=4 c=BNB.xorInteger(a,b)
BitsnByteslib includes the following files:
Related Functions: AndByte, AndShort
Example
Dim a as Integer Dim b as Integer Dim c as Integer a = 63 b = 4 c = BNB.AndInteger(a,b) ' result is 4
Example
Dim a as string Dim c as Integer a = "100" c = BNB.Bin2Dec(a) ' result is 4
Example
Dim buf as String Dim count as Short Dim crc as Integer buf = "Dmitry" count = Len(buf) crc = BNB.CRC32(buf,count,b,0) 'result is 168098323
Related Functions: CylceShiftByte, CycleShiftShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = 65536 b = 16 c = BNB.CycleShiftInteger(a,b) 'result is 1
Example
Dim a as Integer Dim s as String a = 64 s = "123456781234567812345679812345678" BNB.Dec2Bin(a,s) 'result is 1000000
Example
Dim a as String Dim c as Integer a = "12345678901234567890" c = 64 BNB.Dec2Hex(c,a) 'a is "40"
Example
Dim src as String Dim dst as String Dim res as String Dim key as String Dim enc as Short Dim err as Integer src = "ASDFGHJK" dst = "!!!!!!!!" res = "!!!!!!!!" key = "12345678" enc=1 err=BNB.EncryptDES(src,key,dst,enc) 'src is encrypted into dst enc=0 err=BNB.EncryptDES(dst,key,res,enc) 'dst is unencrypted into res
Related Functions: EqvByte, EqvShort
Example
Dim a as Integer Dim b as Integer Dim c as Integer Dim mask as Integer a = 64 b = 62 mask = 2 c = BNB.EqvInteger(a,b,mask) 'result is 2
Related Functions: GetBitInteger, GetBitShort
Example
Dim a as Integer Dim b as Short Dim c as Short a = 64 b = 6 c = BNB.GetBitByte(a,b) 'result is 1
Related Functions: GetBitByte, GetBitInteger
Example
Dim a as Integer Dim b as Short Dim c as Short a = 64 b = 6 c = BNB.GetBitShort(a,b) 'result is 1
Related Functions: GetBitByte, GetBitShort
Example
Dim a as Integer Dim b as Short Dim c as Short a = 64 b = 6 c = BNB.GetBitInteger(a,b) 'result is 1
Related Functions: GetHiWord, GetLowByte, GetHiByte
Example
Dim a as Integer Dim c as Short a = 65537 c = BNB.GetLowWord(a) 'result is 1
Example
Dim a as Integer Dim c as String C = "40" A = BNB.Hex2Dec(C) 'result is 64
Related Functions: InverseBitByte, InverseBitShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = 3 b = 1 c = BNB.InverseBitInteger(a,b) 'result is 1
Related Functions: NotByte, NotShort
Example
Dim a as Integer Dim c as Integer a = 32767 c = BNB.NotInteger(a) 'result is -32768
Related Functions: OrByte, OrShort
Example
Dim a as Integer Dim b as Integer Dim c as Integer a = 3 b = 9 c = BNB.OrInteger(a,b) 'result is 11
Related Functions: ResetBitByte, ResetBitShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = 3 b = 0 c = BNB.ResetBitInteger(a,b) 'result is 2
Related Functions: SetBitByte, SetBitShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = 2 b = 0 c = BNB.SetBitInteger(a,b) 'result is 3
Related Functions: SetHiWord, SetLowByte, SetHighByte
Example
Dim a as Integer Dim b as Short Dim c as Integer a = INT(VAL(FldA.text)) b = INT(VAL(FldB.text)) c = BNB.SetLoWord(a,b)
Related Functions: ShiftByte, ShiftShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = 65536 b = 16 c = BNB.ShiftInteger(a,b) 'result is 0
Related Functions: SignShiftByte, SignShiftShort
Example
Dim a as Integer Dim b as Short Dim c as Integer a = -4 b = 2 c = BNB.SignShiftInteger(a,b) 'result is -16
Example
Dim A as Short Dim B as Short A = 256 b=BNB.Swap16(a) 'B will be 1. (&h01 -> $h01)
Example
Dim A as Integer Dim B as Integer A = 32768 B=BNB.Swap32(a) 'B will be 1. (&h0100 -> $h0001)
Example
Dim A as Double Dim B as Double A = 32768 B=BNB.Swap64(a)
Example
Dim c as Short c = BNB.Version() 'result is 1
Related Functions: XorByte, XorShort
Example
Dim a as Integer Dim b as Integer Dim c as Integer a = 3 b = 9 c = BNB.XorInteger(a,b) 'result is 10
Code is also provide to encrypt and decrypt data on the Windows desktop. This code is in \nsbasic\tools\BitsNBytes_encrypt.zip.
Example
Dim keyStr as String keyStr = "secret" BNB.SetDESKey keystr
Example
Dim plainText as String Dim encryptedText as String plainText = "The quick brown fox jumps over the lazy dog!" encryptedText = BNB.EncryptDESString(plainText)
Example
Dim encryptedText as String Dim plainText as String plainText = BNB.DecryptDESString(encryptedText)
To use it, the EncDec.dll file must be placed in a system directory or some directory that is currently in your PATH. For Visual Basic/VBA, include the EncDec.bas module. For Visual C/C++, include the EncDec.h header file and link with the EncDec.lib library (note that the EncDec.dll is still required).
The EncDec DLL functions have the same restrictions as their BitsNbytes library counterparts. The main differences are that you specify the encryption key with every function call and that the functions return the length of the returned string.
VB/VBA Example
Dim KeyStr As String Dim DecryptedStr As String Dim EncryptedStr As String Dim StrLen As Integer KeyStr = "secret" DecryptedStr = "The quick brown fox jumps over the lazy dog!" StrLen = EncryptData(KeyStr, DecryptedStr, EncryptedStr) DecryptedStr = "" StrLen = DecryptData(KeyStr, EncryptedStr, DecryptedStr)
C/C++ Example
char keyStr[25]; char decryptedStr[251]; char encryptedStr[251]; int len; strcpy(keyStr, "secret"); strcpy(decryptedStr, "The quick brown fox jumps over the lazy dog!"); len = encryptData(keyStr, decryptedStr, encryptedStr); strcpy(decryptedStr, ""); len = decryptData(keyStr, encryptedStr, decryptedStr);