Solved

Translate C code to VB

Posted on 2003-11-19
2
315 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
Comment Utility
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
Comment Utility
Thanks.  This helped my figure out the necessary code.
Dan
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now