Hooper

Hi,

I am coverting a CRC function from C++ to VB. There is bitwise and shifing and I am not sure if I am using VB correctly for this. The CRC works in the C++ code but not in VB.

Below is pasted the C++ version and the VB.

Any suggestions would be appreciated.

Thanks

Jeff

C++ Version

bitmask = 1 << 7;

do

{

if (!(runCRC & 0x8000) ^ !(aChar Idea & bitmask))

{

runCRC <<= 1;

runCRC ^= CRC_Poly;

}

else

runCRC <<= 1;

} while (bitmask >>= 1);

VB Version

bitMask = 1 << 7

Do

If (Not (runCRC And &H8000) Xor (Not ((byteValue) And bitMask))) Then

runCRC <<= 1

runCRC = (runCRC Xor CRC_Poly)

Else

runCRC <<= 1

End If

bitMask >>= 1

Loop Until (bitMask = 1)



Re: Visual Basic Language CRC

nobugz

Try:
Loop Until bitMask = 0






Re: Visual Basic Language CRC

Hooper

Hi,

That did not help. What happens is at some point during the CRC calc loop the running CRC, runCRC becomes 32771, and it never changes after that. Gets locked in at that value and the left shift and exclusive OR keeps it there. 32771 is an incorrect CRC for the file I am testing it on.

Thanks for the response.

Jeff





Re: Visual Basic Language CRC

nobugz

You need to show us the declarations of your variables. In both languages.





Re: Visual Basic Language CRC

Hooper

Hi,

Sorry, I was focusing in on that loop thinking I was not doing that portion correct.

The CRC function is called from a Do/While loop. It is passed 2 characters/call from a string, the string it is reading from is all in hex, s28Data in this case. The 2 characters are converted to an int value and then CRCed, ex. 16 will become 22.

The original CRC_16() function was authored by someone else. It is in MFC, a series of function calls which I have pasted below the VB code. We have used this CRC in a couple of other apps as is and I have also changed it to C++.NET. works fine.

Jeff

Dim pcCRC As UInt16

pcCRC = &HFFFF

Do

'read 2 characters (i byte data) at a time

dataByte = Mid(_s28Data, dCount, 2)

dCount = dCount + 2

pcCRC = CRC_16(pcCRC, dataByte, 2)

address = address + 1

Loop Until address >= (START_ADDRESS + romBytes)

//VB version

Private Function CRC_16(ByVal runCRC As UInt16, ByVal str As String, ByVal len As UInt16) As UInt16

Dim CRC_Poly As UInt16 = &H8005

Dim bitMask As Integer

Dim byteValue As Integer

byteValue = [Integer].Parse(str, Globalization.NumberStyles.HexNumber)

bitMask = 1 << 7

Do

If (Not (runCRC And &H8000) Xor (Not ((byteValue) And bitMask))) Then

runCRC <<= 1

' runCRC = (runCRC Xor CRC_Poly)

runCRC ^= CRC_Poly

Else

runCRC <<= 1

End If

bitMask >>= 1

Loop Until (bitMask = 1)

Return runCRC

End Function ' end CRC_16

//MFC Version

#define HexDigit(ch) (int)(isdigit(ch) ch - '0' : toupper(ch) - 'A' + 10)

unsigned HexWord(CString str)

{

return HexDigit(str [0]) * 16 + HexDigit(str [1]);

}

bool HexStr2Bin(CString in, CString &out)

{

int len = in.GetLength();

if (len % 2 != 0)

return false;

for (int i = 0; i < len; i += 2)

out += (char)HexWord(in.Mid(i, 2));

return true;

}

unsigned int CRC_16(unsigned int runCRC, CString str, unsigned int len)

{

unsigned int CRC_Poly = 0x8005;

unsigned int bitmask;

byte *aChar = (byte *)str.GetBuffer(str.GetLength());

for (unsigned i = 0; i < len; i++)

{

bitmask = 1 << 7;

do

{

if (!(runCRC & 0x8000) ^ !(aChar Idea & bitmask))

{

runCRC <<= 1;

runCRC ^= CRC_Poly;

}

else

runCRC <<= 1;

} while (bitmask >>= 1);

}

return runCRC;

}

STDMETHODIMP CCRCCalc::CRC16(long CRC, BSTR asciiBuf, short arraySize, long *retCRC)

{

USES_CONVERSION;

CString temp;

HexStr2Bin(W2A(asciiBuf), temp);

unsigned short tc = CRC;

if (tc == -1)

tc = 0xFFFF;

tc = CRC_16(tc, temp, arraySize / 2);

//retCRC = new long;

*retCRC = (long)tc;

return S_OK;

}





Re: Visual Basic Language CRC

Hooper

Hi,

During testing the VB code above it appears that as it is running at some point the line,

runCRC <<= 1, ceases to be correct. The left shifted value is wrong.

Jeff





Re: Visual Basic Language CRC

nobugz

C++'s "unsigned int" is equivalent to VB's UInt32. You are losing bit #15.





Re: Visual Basic Language CRC

Hooper

Hi,

I changed all the UInt16 to UInt32 and that did not fix it. I have a file that I know the CRC should be 8C44. The VB code is saying the CRC is 8003. Changing to UInt32 made the CRC FFFF8003.

Thanks for the input.

Jeff





Re: Visual Basic Language CRC

Hooper

Hi,

I believe that the CRC problem cannot be in this function. Can find nothing wrong with it. So I am going to mark this as answered.

Jeff