Solved

Translate C code to VB

Posted on 2003-11-19
2
324 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

This is an explanation of a simple data model to help parse a JSON feed
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …

792 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