Any old school basic guys still out there?

Been using C for so dam long now, i cant even think straight converting this function to basic.
IF anybody has the time and idea what i may have jacked up feel free to comment.



BOOL CheckCRC( int length )
{
	WORD crc=0;
	BYTE data;
	WORD x,total;

	// get WORD value for the CRC data in response
	total =  (BYTE)vp2buffer[length - 2];
	total = total << 8;
	total |= (BYTE)vp2buffer[length - 1];

	// Dont CRC the last 2 bytes of the data
	length -=2;

	for(x=1; x < length;x++)
	{
		data = vp2buffer[x];
		crc = crc_table [(crc >> 8) ^ data] ^ (crc << 8);
	}

	if(crc == total)
	{
		return true;
	}

	return false;
}

as best i can convert it, i came up with this, but it does not work.
There are suttle differences as i am passing the data string this time to the basic function, whereas the C function the data was global


Function Check_CRC(string_data as string, length as word) as boolean

	dim crc as word
	dim data as byte
	dim x as word
	dim total as word
	dim a,b as word
	
	
	crc = 0


	' get WORD value for the CRC data in response
'	total =  (BYTE)vp2buffer[length - 2];
'	total = total << 8;
'	total |= (BYTE)vp2buffer[length - 1];

	total = asc(mid(string_data,length - 2,1))
	total = total * &h80
	total = total + asc(mid(string_data,length - 1,1))
	

	' Dont CRC the last 2 bytes of the data

	'length -=2;
	length = length - 2
	

	'for(x=1; x < length;x++)
	for x=1 to length 

		'data = vp2buffer[x];
		data = asc(mid(string_data,x,1))
			
		'crc = crc_table [(crc >> 8) ^ data] ^ (crc << 8);
		
		
		'(crc >> 8) ^ data
		a = (crc / &h80 AND &h00ff) XOR data
		
		'(crc << 8)
		b = crc * &h80 AND &hff00
		
		
			
		crc = crc + ( crc_table(a) XOR b)
		
		
	Next x


	printf("Total = " + str(total) + "\r\n")
	printf("CRC = " + str(CRC) + "\r\n")


	If crc = total Then
	
		Check_CRC = true
	Else
	
		Check_CRC = false
		
	End If

End Function

Try this, I dont know if you need to replace the ‘Xor’ call with a ^

Private Function CheckCRC(data As String, length As Integer) As BOOL
	Dim crc As WORD = 0
	Dim data As [BYTE]
	Dim x As WORD, total As WORD

	' get WORD value for the CRC data in response
	total = DirectCast(vp2buffer(length - 2), [BYTE])
	total = total << 8
	total = total Or DirectCast(vp2buffer(length - 1), [BYTE])

	' Dont CRC the last 2 bytes of the data
	length -= 2

	For x = 1 To length - 1
		data = vp2buffer(x)
		crc = crc_table((crc >> 8) Xor data) Xor (crc << 8)
	Next

	If crc = total Then
		Return True
	End If

	Return False
End Function

@ Rajesh - Thanks, but this is old, old school basic. which does not allow <<, >>, ^

The main thing i want to get is this line

crc = crc_table [(crc >> 8) ^ data] ^ (crc << 8);

I got it working now.

FWIW i had 2 things messed up.

#1) to shift left or right by 8 is &h100 and not &h80 like i had it.
#2) when using the mid statement, unlike C the first element is 0 in an array, but in basic its 1.

(offtopic)

That’s not old school basic, just plain old basic…

10 print "hello world"
20 goto 10

That’s old school basic :slight_smile:

Very true, i forgot about line numbers. seeing that makes me thing about my first computer. The C64

I played a joke on my teacher once and did this:

10 cls
20 goto 10

I said my computer (TRS-80) is broken; then hit Ctrl-Break and went “hahaha”

I was trouble from the start! :smiley: