Cakkie
August 29th, 2001, 07:33 AM
hi, I'm doing some encoding here, and I have a little problem. I am converting 3 bytes into 4. All works fine, but the problem is that I'm going via strings, and I would like to get rid of that. For that, I need a command in VB that shifts bits. In Java and C++ you have the << and >> operators, Fortran has LSHFT and RSHFT, Foxpro has BITLSHIFT and BITRSHIFT, but they don't seem to exists in VB. Below is the code I use.
arrToEncode is a byte array with 3 elements. ascToBinArr and BinToAscArr are collections that speed up converting from asci to a string in binary form (like 01110100) and the other way around. At the end, I need a byte array with 4 elements.
Anyone knows how to do this a little faster?
Dim strBin24 as string, strBin32 as string, strBase64 as string
strBin24 = AscToBinArr(arrToEncode(0)) & _
AscToBinArr(arrToEncode(1)) & _
AscToBinArr(arrToEncode(2))
strBin32 = "00" & mid(strBin24, 1, 6) & _
"00" & mid(strBin24, 7, 6) & _
"00" & mid(strBin24, 13, 6) & _
"00" & mid(strBin24, 19, 6)
strBase64 = Base64Arr(BinToAscArr(mid(strBin32, 1, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 9, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 17, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 25, 8)))
More info about the encoding.
It is Base64 Encoding, quite easy, just slow.
Make 3 bytes into 4.
Take 3 bytes and compute binary value.
Take the first six bits of the first byte, prefix with 00
Take last two bits of first byte, four first of second byte, prefix with 00
Take last four of second, two first of third, prefix with 00
Take last six of third, prefix with 00
Just put this to an example:
we have the word 'get'
the ascii values of the 3 bytes will be 71 69 84
binary, that would make 01000111 01000101 01010100
take 6 bits, and prefix with 00 gives 00010001
next 6 bits, and prefix with 00 gives 00110100
next 6 bits, and prefix with 00 gives 00010101
last 6 bits, and prefix with 00 gives 00010100
So, or four bytes look like this 00010001 00110100 00010101 00010100
or back in ascii: 17 52 21 20
or just 4
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
arrToEncode is a byte array with 3 elements. ascToBinArr and BinToAscArr are collections that speed up converting from asci to a string in binary form (like 01110100) and the other way around. At the end, I need a byte array with 4 elements.
Anyone knows how to do this a little faster?
Dim strBin24 as string, strBin32 as string, strBase64 as string
strBin24 = AscToBinArr(arrToEncode(0)) & _
AscToBinArr(arrToEncode(1)) & _
AscToBinArr(arrToEncode(2))
strBin32 = "00" & mid(strBin24, 1, 6) & _
"00" & mid(strBin24, 7, 6) & _
"00" & mid(strBin24, 13, 6) & _
"00" & mid(strBin24, 19, 6)
strBase64 = Base64Arr(BinToAscArr(mid(strBin32, 1, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 9, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 17, 8))) & _
Base64Arr(BinToAscArr(mid(strBin32, 25, 8)))
More info about the encoding.
It is Base64 Encoding, quite easy, just slow.
Make 3 bytes into 4.
Take 3 bytes and compute binary value.
Take the first six bits of the first byte, prefix with 00
Take last two bits of first byte, four first of second byte, prefix with 00
Take last four of second, two first of third, prefix with 00
Take last six of third, prefix with 00
Just put this to an example:
we have the word 'get'
the ascii values of the 3 bytes will be 71 69 84
binary, that would make 01000111 01000101 01010100
take 6 bits, and prefix with 00 gives 00010001
next 6 bits, and prefix with 00 gives 00110100
next 6 bits, and prefix with 00 gives 00010101
last 6 bits, and prefix with 00 gives 00010100
So, or four bytes look like this 00010001 00110100 00010101 00010100
or back in ascii: 17 52 21 20
or just 4
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook