Solved

Translate C code to VB

Posted on 2003-11-19
2
325 Views
Last Modified: 2010-04-17
Can someone translate this to VB?

Example code written in C
FILE * filefd;
int nVal;
char buf[5];
int nCksum = 0;
filefd=fopen("BATCH-FILE","r+");
if (filefd==NULL)
{
perror("Error opening file");
}
else
{
while ((nVal = fgetc(filefd)) != EOF)                    
{                            
      if (nVal != 10 && nVal != 13)
      {                          
nCksum =(((nCksum&1)<<15)+(nCksum>>1)+nVal)&0xffff;
            }
}
sprintf(buf,"%05d",nCksum);
fwrite(buf,1,sizeof(buf),filefd);
}

Here's the psuedo code.

Checksum Algorithm
NOTE: The checksum should be appended to the File Trailer Record after the checksum for the file has been calculated.
Starting at the beginning of the file and given the variables nCksum, cVar, and nVal, the following pseudo code shows how to calculate the checksum of a file (linefeeds and carriage returns are ignored):

nCksum = zero
While Not End of File
{
      cVar = next character in file
      
      
      If cVar != Linefeed And cVar != Carriage Return
      nVal = ASCII value of cVar
            nCksum = ((nCksum%2)*(2^15)+(nCksum/2)+nVal)%65536
      Endif

}
Append nCksum to file
0
Comment
Question by:mikodmc
2 Comments
 
LVL 1

Accepted Solution

by:
vbs03 earned 250 total points
ID: 9785864
Hi,
This function should do the needful.

Sub temp()
Dim Cksum As Integer, filenum As Integer
Dim var As String
Cksum = 0
filenum = FreeFile()
Open "BATCH-FILE" For Input As filenum
While Not EOF(filenum)
    Get filenum, , var
    If var <> vbCr And var <> vbLf Then
        Cksum = ((Cksum Mod 2) * (2 ^ 15) + (Cksum / 2) + Val(var)) Mod 65536
    End If
Wend
Close filenum
Open "C:\emp.txt" For Append As #1
Put 1, , Cksum
Close filenum
End Sub

In case there are any issues then u fix them. Atleast u have got a start now.
Regards,
VBS
0
 

Author Comment

by:mikodmc
ID: 9827110
Thanks.  This helped my figure out the necessary code.
Dan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question