?
Solved

Translate C code to VB

Posted on 2003-11-19
2
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
vbs03 earned 750 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
Simple Linear Regression
Suggested Courses

765 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