Notices
Non Scooby Related Anything Non-Scooby related

any c programmers out there?

Thread Tools
 
Search this Thread
 
Old 05 August 2001, 05:34 PM
  #1  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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;
}
Old 05 August 2001, 06:28 PM
  #2  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Cool

If I could write VB I would

Sorry

Is it the syntax you want or the understanding of what it does? I can help with the latter, but being a C/C++ bod I can't help with the former.
Old 05 August 2001, 06:55 PM
  #3  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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.
Old 06 August 2001, 12:00 AM
  #4  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

good point, vb ints hold up to 16K. any ideas on pc (MSVC) based 'c' ints then?
Old 06 August 2001, 12:15 AM
  #5  
Dizzy
Scooby Regular
 
Dizzy's Avatar
 
Join Date: May 2001
Posts: 2,537
Likes: 0
Received 0 Likes on 0 Posts
Post

sucked from msdn....
char, unsigned char, signed char 1 byte
short, unsigned short 2 bytes
int, unsigned int 4 bytes
long, unsigned long 4 bytes
float 4 bytes
double 8 bytes
long double1 8 bytes
Old 06 August 2001, 09:18 AM
  #6  
Dizzy
Scooby Regular
 
Dizzy's Avatar
 
Join Date: May 2001
Posts: 2,537
Likes: 0
Received 0 Likes on 0 Posts
Question

Still want it translatin? (urg VB )
if u have the code just dont call the function and hop round the check
Old 06 August 2001, 10:57 AM
  #7  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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
Old 06 August 2001, 11:46 AM
  #8  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Cool

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.
Old 06 August 2001, 01:00 PM
  #9  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Talking

Cor! Anyone would think we knew what we were talking about

Fosters, what game is it for? This is one cat that isn't gonna get killed by curiosity
Old 06 August 2001, 01:31 PM
  #10  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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!
Old 06 August 2001, 01:40 PM
  #11  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Cool

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.
Old 06 August 2001, 02:05 PM
  #12  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

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
Old 06 August 2001, 02:11 PM
  #13  
Rob Walker
Scooby Regular
 
Rob Walker's Avatar
 
Join Date: Nov 1999
Location: Stockport
Posts: 474
Likes: 0
Received 0 Likes on 0 Posts
Post

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!

Old 06 August 2001, 02:57 PM
  #14  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Talking

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).]
Old 06 August 2001, 03:08 PM
  #15  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

xor? oops! changed code and I get different, but wrong values still!

Old 06 August 2001, 03:09 PM
  #16  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

hanslow, you have email, btw.
Old 06 August 2001, 03:18 PM
  #17  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Cool

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.
Old 06 August 2001, 03:23 PM
  #18  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Post

Can you send me your pilot file to hanslosl@bemailhost.marconicomms.com cos I can't seem to get it from my NTL Webmail.

Cheers
Old 06 August 2001, 03:34 PM
  #19  
Fosters
Scooby Regular
Thread Starter
 
Fosters's Avatar
 
Join Date: Jul 2000
Location: Islington
Posts: 2,145
Likes: 0
Received 0 Likes on 0 Posts
Post

Done.

Old 06 August 2001, 04:01 PM
  #20  
Hanslow
Scooby Regular
 
Hanslow's Avatar
 
Join Date: Mar 2001
Location: Derbyshire
Posts: 4,496
Likes: 0
Received 0 Likes on 0 Posts
Cool

You have mail mate, including a pilot with 500,000,000 credits

Don't spend it all at once...make sure you get a ship with a big fat TURBO!

Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Nick_Cat
Computer & Technology Related
2
26 September 2015 08:00 AM
Alan Jeffery
Non Scooby Related
1
13 September 2015 01:21 PM
Trout
ScoobyNet General
72
30 November 2000 08:23 AM



Quick Reply: any c programmers out there?



All times are GMT +1. The time now is 06:24 AM.