HRCIII
asked on
Code translation C to VB problem
I am attempting to recreate a bit of C code into VB code however it is not cooperating. I have sample input and output of what the results should be in C lanugage but I have been unable to make a function in VB6 which will emulate the action. Simply put, I need to create a 16 bit CRC CCITT check that will produce the following...
'// The following code is what I intend to accomplish with actual inputs and actual results in VB6
'//BEGIN
B(0) = 0
B(1) = 88
By(2) = 1
s = CRC16(b)
s = "47EB"
'//END
'// The following code is what I have been provided with in C language
// BEGIN
unsigned short CRC(unsigned char *s, int len, unsigned
short crcv)
{
register unsigned c,q;
for (; len; len--)
{
c = *s++;
q = (crcv ^ c) & 017;
crcv = (crcv >> 4) ^ (q * 010201);
q = (crcv ^ (c >> 4)) & 017;
crcv = (crcv >> 4) ^ (q * 010201);
}
return (crcv);
}
//END
'// So far I have been unable to get any translated code to work in VB6, I always get overflows. I am not skilled with C languages unfortuantly.
'// Any help would be greatly appreciated
'// The following code is what I intend to accomplish with actual inputs and actual results in VB6
'//BEGIN
B(0) = 0
B(1) = 88
By(2) = 1
s = CRC16(b)
s = "47EB"
'//END
'// The following code is what I have been provided with in C language
// BEGIN
unsigned short CRC(unsigned char *s, int len, unsigned
short crcv)
{
register unsigned c,q;
for (; len; len--)
{
c = *s++;
q = (crcv ^ c) & 017;
crcv = (crcv >> 4) ^ (q * 010201);
q = (crcv ^ (c >> 4)) & 017;
crcv = (crcv >> 4) ^ (q * 010201);
}
return (crcv);
}
//END
'// So far I have been unable to get any translated code to work in VB6, I always get overflows. I am not skilled with C languages unfortuantly.
'// Any help would be greatly appreciated
Hi HRCIII,
The difficulty you will have is matching the exact number of bits of the original algorithm. I cant see anything that suggests a 32 bit architecture but its certainly 16bit or more because octal 010201 is 16 bits wide.
Paul
The difficulty you will have is matching the exact number of bits of the original algorithm. I cant see anything that suggests a 32 bit architecture but its certainly 16bit or more because octal 010201 is 16 bits wide.
Paul
ASKER
I VB very well but I know little to nothing about C, if it helps, the input data as viewed in hex comes in as three bytes like this..
'01 58 00'
'01 58 00'
Hi HRCIII,
Ok! You be the VB guru (is there a song in that?) and throw something together that does what my comments describe and between us we'll crack it. I know enough VB to read it but I dont write it too quickly.
Paul
Ok! You be the VB guru (is there a song in that?) and throw something together that does what my comments describe and between us we'll crack it. I know enough VB to read it but I dont write it too quickly.
Paul
ASKER
Unfortuantly I cant spell or do grammer either :).
Using the following function I still have an Overflow error occurring. I am calling the function using the following example code.
'///// Calling code using a command button //////
Private Sub cmdTestCRC_Click()
Dim s, n As Integer
Dim b(2) As Byte
b(0) = 1
b(1) = 88
b(2) = 0
s = CRC16(b, n)
MsgBox s
End Sub
'///////////////// END ///////////
Function CRC16(ByRef s() As Byte, ByVal crcv As Integer) As Integer
Dim c As Byte, q As Long
Dim i As Integer
For i = LBound(s) To UBound(s)
c = s(i)
q = (crcv Xor c) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
q = (crcv Xor Fix(c / 16)) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
Next i
CRC16 = crcv
End Function
Using the following function I still have an Overflow error occurring. I am calling the function using the following example code.
'///// Calling code using a command button //////
Private Sub cmdTestCRC_Click()
Dim s, n As Integer
Dim b(2) As Byte
b(0) = 1
b(1) = 88
b(2) = 0
s = CRC16(b, n)
MsgBox s
End Sub
'///////////////// END ///////////
Function CRC16(ByRef s() As Byte, ByVal crcv As Integer) As Integer
Dim c As Byte, q As Long
Dim i As Integer
For i = LBound(s) To UBound(s)
c = s(i)
q = (crcv Xor c) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
q = (crcv Xor Fix(c / 16)) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
Next i
CRC16 = crcv
End Function
Hi HRCIII,
Where does it overflow?
Looks like what you've got is close. I cant see any problems with at first sight.
Paul
Where does it overflow?
Looks like what you've got is close. I cant see any problems with at first sight.
Paul
Dim b(2) As Byte
b(0) = 1
b(1) = 88
b(2) = 0
Can you do that in VB? Create an array length 2 and put 3 items in it? Or is that an array of length 3?
Paul
b(0) = 1
b(1) = 88
b(2) = 0
Can you do that in VB? Create an array length 2 and put 3 items in it? Or is that an array of length 3?
Paul
Erm...
Dim s, n As Integer
Dim b(2) As Byte
b(0) = 1
b(1) = 88
b(2) = 0
s = CRC16(b, n)
You dont seem to have initialised 'n'. Surely it should be 3?
Paul
Dim s, n As Integer
Dim b(2) As Byte
b(0) = 1
b(1) = 88
b(2) = 0
s = CRC16(b, n)
You dont seem to have initialised 'n'. Surely it should be 3?
Paul
ASKER
Function CRC16(ByRef s() As Byte, ByVal crcv As Integer) As Integer
Dim c As Byte, q As Long
Dim i As Integer
For i = LBound(s) To UBound(s)
c = s(i)
q = (crcv Xor c) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF& '//OVERFLOW ON THIS LINE
q = (crcv Xor Fix(c / 16)) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
Next i
CRC16 = crcv
End Function
Write C code to Dll and call it using VB API calls feature. If something is working, don't touch it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I highlighted the line which overflowing below...
Function CRC16(ByRef s() As Byte, ByVal crcv As Integer) As Integer
Dim c As Byte, q As Long
Dim i As Integer
For i = LBound(s) To UBound(s)
c = s(i)
q = (crcv Xor c) And &O17&
>> crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
q = (crcv Xor Fix(c / 16)) And &O17&
crcv = Fix(crcv / 16) Xor (q * &O10201&) And &HFFFF&
Next i
CRC16 = crcv
End Function
So its one of the calculations in that expression. My suggestion should find out which one.
Paul
Paul
ASKER
I would gladly use API calls to a DLL containing the function but I have no capability for accomplishing this, I would gladly pay someone to take care of it, unfortunatly I don't know anyone who can.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm no VB expert but I can interpret the C for you:
// Ignore 'register'. c and q are integers.
register unsigned c,q;
// Count 'len' down to zero, repeating the contents of the brace.
for (; len; len--)
{
// Collect the next character into 'c'.
c = *s++;
// q = (crcv xor c) bitwise anded with octal 17 (hex 0f, decimal 15).
q = (crcv ^ c) & 017;
// crcv becomes (crcv bitshifted right 4 bits) xor (q * octal 10201),
crcv = (crcv >> 4) ^ (q * 010201);
// q = ( crcv xor ( c bitshift right 4 bits ) ) bitwise anded with octal 17 (hex 0f, decimal 15)
q = (crcv ^ (c >> 4)) & 017;
// Same process as above.
crcv = (crcv >> 4) ^ (q * 010201);
}
// result of function is crcv.
return (crcv);
}
Paul