Solved

Translate C code to VB

Posted on 2003-11-19
2
320 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 115
Recommendation vb6 to vb.net or others 14 112
Apps blocked by Java 9 64
Please help| Getting the syntax error in below query. 2 47
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is an explanation of a simple data model to help parse a JSON feed
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

21 Experts available now in Live!

Get 1:1 Help Now