any c programmers out there?
can you translate this code into VB for me?
function is passed a 256byte block...
int checksum (unsigned char *block)
{
int acc,carry;
int i;
acc = 0x49;
carry = 0;
for (i = 0x49; i > 0; i--)
{
acc += block[i-1] + carry;
carry = acc >> 8;
acc &= 255;
acc ^= block[i];
}
return acc;
}
function is passed a 256byte block...
int checksum (unsigned char *block)
{
int acc,carry;
int i;
acc = 0x49;
carry = 0;
for (i = 0x49; i > 0; i--)
{
acc += block[i-1] + carry;
carry = acc >> 8;
acc &= 255;
acc ^= block[i];
}
return acc;
}
I think I've got it. I needed a bit of code for the bit shift, but VB doesn't handle that.
cheers for the response though.
I'm hacking a game config file and managed to get hold of the code! tee hee.
cheers for the response though.
I'm hacking a game config file and managed to get hold of the code! tee hee.
Cheers Dizzy, I wrote the code to do the bit shift, but unless I'm mistaken what's the point in shifting a byte 8 bits - don't you just end up with 0?
If you could put something together I'd appreciate it - especially to check against what I've written.
Fosters
can you mail me at Michael.toye@bmw.co.uk if you're not going to post.
cheers
If you could put something together I'd appreciate it - especially to check against what I've written.
Fosters
can you mail me at Michael.toye@bmw.co.uk if you're not going to post.
cheers
Trending Topics
Like they say...Size matters 
All depends on the size of your ints. On our unix system, the ints are by default, 32 bit. So shifting by 8 bits still gives you 24 bits to play around with.
Unless the int is defined to definitely be an 8 bit value (which I doubt, who would want an int that only went up to 255 unsigned, or 127, -128 signed) then it will be at least 16 bit.
This is from a C/C++ standpoint, dunno about VB.

All depends on the size of your ints. On our unix system, the ints are by default, 32 bit. So shifting by 8 bits still gives you 24 bits to play around with.
Unless the int is defined to definitely be an 8 bit value (which I doubt, who would want an int that only went up to 255 unsigned, or 127, -128 signed) then it will be at least 16 bit.
This is from a C/C++ standpoint, dunno about VB.
Can't get the 'kin code to work! git!
The game is the retro BBC Elite by New Kind (www.newkind.co.uk). You start off a new game with the standard 100credits. This is the point where I started to disagree with it big time!
If anyone wants to check my code, then feel free to ask!
The game is the retro BBC Elite by New Kind (www.newkind.co.uk). You start off a new game with the standard 100credits. This is the point where I started to disagree with it big time!
If anyone wants to check my code, then feel free to ask!
I'm intrigued even more now
I'll have a look at your code if it's understandable? Bear in mind I don't do or know VB (I can do Spectrum Basic tho
). Other than that, I'm a pure C/C++ bod.
If it is the theory that ain't workin, I can have a look, failing that I can't help (which ain't much use
)
Either post it or send it to my email in profile.
I'll have a look at your code if it's understandable? Bear in mind I don't do or know VB (I can do Spectrum Basic tho
). Other than that, I'm a pure C/C++ bod.If it is the theory that ain't workin, I can have a look, failing that I can't help (which ain't much use
)Either post it or send it to my email in profile.
Here's the original c code for the checksum routine used to read and save commander files... My vb code follows...
Fosters
C code ================================================== ========
int checksum (unsigned char *block)
{
int acc,carry;
int i;
acc = 0x49;
carry = 0;
for (i = 0x49; i > 0; i--)
{
acc += block[i-1] + carry;
carry = acc >> 8;
acc &= 255;
acc ^= block[i];
}
return acc;
}
Save commander file =============================================
int save_commander_file (char *path)
{
FILE *fp;
unsigned char block[256];
int i;
int chk;
fp = fopen (path, "wb");
if (fp == NULL)
return 1;
block[0] = cmdr.mission;
block[1] = docked_planet.d;
block[2] = docked_planet.b;
block[3] = cmdr.galaxy.a;
block[4] = cmdr.galaxy.b;
block[5] = cmdr.galaxy.c;
block[6] = cmdr.galaxy.d;
block[7] = cmdr.galaxy.e;
block[8] = cmdr.galaxy.f;
block[9] = (cmdr.credits >> 24) & 255;
block[10] = (cmdr.credits >> 16) & 255;
block[11] = (cmdr.credits >> 8) & 255;
block[12] = cmdr.credits & 255;
block[13] = cmdr.fuel;
block[14] = 4;
block[15] = cmdr.galaxy_number;
block[16] = cmdr.front_laser;
block[17] = cmdr.rear_laser;
block[18] = cmdr.left_laser;
block[19] = cmdr.right_laser;
block[20] = 0;
block[21] = 0;
block[22] = cmdr.cargo_capacity + 2;
for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
block[23+i] = cmdr.current_cargo[i];
block[40] = cmdr.ecm ? 255 : 0;
block[41] = cmdr.fuel_scoop ? 255 : 0;
block[42] = cmdr.energy_bomb ? 0x7F : 0;
block[43] = cmdr.energy_unit;
block[44] = cmdr.docking_computer ? 255 : 0;
block[45] = cmdr.galactic_hyperdrive ? 255 : 0;
block[46] = cmdr.escape_pod ? 255 : 0;
block[47] = 0;
block[48] = 0;
block[49] = 0;
block[50] = 0;
block[51] = cmdr.missiles;
block[52] = cmdr.legal_status;
for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
block[53+i] = stock_market[i].current_quantity;
block[70] = cmdr.market_rnd;
block[71] = cmdr.score & 255;
block[72] = cmdr.score >> 8;
block[73] = 0x20;
chk = checksum (block);
block[74] = chk ^ 0xA9;
block[75] = chk;
for (i = 76; i < 256; i++)
block[i] = 0;
if (fwrite (block, 256, 1, fp) != 1)
return 1;
if (fclose (fp) == EOF)
return 1;
return 0;
}
'Visual Basic stuff
'Main code ================================================== ====
Dim sIn As String * 256
Dim acc As Double
Dim carry As Double
Dim i As Integer
sIn = FileText(Text1) 'read in 256byte file
acc = Asc(Mid(sIn, 50, 1))
carry = 0
For i = 50 To 2 Step -1 'string offsets work from 1, not 0
acc = acc + Asc(Mid(sIn, i - 1, 1)) + carry
carry = BinToDec(Left(String(8, "0") & DecToBin(acc), 64))
acc = acc And 255
acc = acc Or Asc(Mid(sIn, i, 1))
Next i
Label4 = "Checksum (byte 74, 75)" & vbCrLf
'sub functions =================================================
Private Function DecToBin(ByVal dIn As Double) As String
DecToBin = ""
While dIn >= 1
DecToBin = IIf(dIn Mod 2 = 0, "0", "1") & DecToBin
dIn = dIn 2
Wend
End Function
Private Function BinToDec(ByVal sIn As String) As Double
Dim x As Integer
BinToDec = 0
For x = 1 To Len(sIn)
BinToDec = BinToDec + (CInt(Mid(sIn, x, 1)) * (2 ^ (Len(sIn) - x)))
Next x
End Function
Fosters
C code ================================================== ========
int checksum (unsigned char *block)
{
int acc,carry;
int i;
acc = 0x49;
carry = 0;
for (i = 0x49; i > 0; i--)
{
acc += block[i-1] + carry;
carry = acc >> 8;
acc &= 255;
acc ^= block[i];
}
return acc;
}
Save commander file =============================================
int save_commander_file (char *path)
{
FILE *fp;
unsigned char block[256];
int i;
int chk;
fp = fopen (path, "wb");
if (fp == NULL)
return 1;
block[0] = cmdr.mission;
block[1] = docked_planet.d;
block[2] = docked_planet.b;
block[3] = cmdr.galaxy.a;
block[4] = cmdr.galaxy.b;
block[5] = cmdr.galaxy.c;
block[6] = cmdr.galaxy.d;
block[7] = cmdr.galaxy.e;
block[8] = cmdr.galaxy.f;
block[9] = (cmdr.credits >> 24) & 255;
block[10] = (cmdr.credits >> 16) & 255;
block[11] = (cmdr.credits >> 8) & 255;
block[12] = cmdr.credits & 255;
block[13] = cmdr.fuel;
block[14] = 4;
block[15] = cmdr.galaxy_number;
block[16] = cmdr.front_laser;
block[17] = cmdr.rear_laser;
block[18] = cmdr.left_laser;
block[19] = cmdr.right_laser;
block[20] = 0;
block[21] = 0;
block[22] = cmdr.cargo_capacity + 2;
for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
block[23+i] = cmdr.current_cargo[i];
block[40] = cmdr.ecm ? 255 : 0;
block[41] = cmdr.fuel_scoop ? 255 : 0;
block[42] = cmdr.energy_bomb ? 0x7F : 0;
block[43] = cmdr.energy_unit;
block[44] = cmdr.docking_computer ? 255 : 0;
block[45] = cmdr.galactic_hyperdrive ? 255 : 0;
block[46] = cmdr.escape_pod ? 255 : 0;
block[47] = 0;
block[48] = 0;
block[49] = 0;
block[50] = 0;
block[51] = cmdr.missiles;
block[52] = cmdr.legal_status;
for (i = 0; i < NO_OF_STOCK_ITEMS; i++)
block[53+i] = stock_market[i].current_quantity;
block[70] = cmdr.market_rnd;
block[71] = cmdr.score & 255;
block[72] = cmdr.score >> 8;
block[73] = 0x20;
chk = checksum (block);
block[74] = chk ^ 0xA9;
block[75] = chk;
for (i = 76; i < 256; i++)
block[i] = 0;
if (fwrite (block, 256, 1, fp) != 1)
return 1;
if (fclose (fp) == EOF)
return 1;
return 0;
}
'Visual Basic stuff
'Main code ================================================== ====
Dim sIn As String * 256
Dim acc As Double
Dim carry As Double
Dim i As Integer
sIn = FileText(Text1) 'read in 256byte file
acc = Asc(Mid(sIn, 50, 1))
carry = 0
For i = 50 To 2 Step -1 'string offsets work from 1, not 0
acc = acc + Asc(Mid(sIn, i - 1, 1)) + carry
carry = BinToDec(Left(String(8, "0") & DecToBin(acc), 64))
acc = acc And 255
acc = acc Or Asc(Mid(sIn, i, 1))
Next i
Label4 = "Checksum (byte 74, 75)" & vbCrLf
'sub functions =================================================
Private Function DecToBin(ByVal dIn As Double) As String
DecToBin = ""
While dIn >= 1
DecToBin = IIf(dIn Mod 2 = 0, "0", "1") & DecToBin
dIn = dIn 2
Wend
End Function
Private Function BinToDec(ByVal sIn As String) As Double
Dim x As Integer
BinToDec = 0
For x = 1 To Len(sIn)
BinToDec = BinToDec + (CInt(Mid(sIn, x, 1)) * (2 ^ (Len(sIn) - x)))
Next x
End Function
Can't you call a C function from Visual Basic? Or compile the function into a dll or something like that.. I remember calling assembly language routines from vb but that was about 5 years ago now and I can't remember how!
Question.....
Apart from that being all gobbledegook to a C programmer (well bits of it anyway)....
Did you know that <B>acc ^= block[ i ]</B> (hope the array index doesn't turn on italics!) is actually an EXCLUSIVE OR. i.e. 1,0 and 0,1 will return 1, 1,1 and 0,0 will return 0.
Just wondered whether <B>Or</B> in VB is exclusive or standard.
Edit : bit of tidying up
[This message has been edited by Hanslow (edited 06 August 2001).]
Apart from that being all gobbledegook to a C programmer (well bits of it anyway)....
Did you know that <B>acc ^= block[ i ]</B> (hope the array index doesn't turn on italics!) is actually an EXCLUSIVE OR. i.e. 1,0 and 0,1 will return 1, 1,1 and 0,0 will return 0.
Just wondered whether <B>Or</B> in VB is exclusive or standard.
Edit : bit of tidying up
[This message has been edited by Hanslow (edited 06 August 2001).]
Just checked me mail, I'll have a quick go whilst I am here at work, it's much more interesting than normal mundane stuff 
I'll email you back my results so you can try them.....
Should be a doddle with the original source code being in C.

I'll email you back my results so you can try them.....
Should be a doddle with the original source code being in C.
Thread
Thread Starter
Forum
Replies
Last Post



)
