Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 17974
Number of posts: 55346

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C to VB Posted by brutallica on 27 Feb 2005 at 8:04 AM
Hi,

Is there anyone here who could translate this C Code into VB? Or is it possible to link VB and C/C++ codes using the MSVisual Studio?? Thanks!

#define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))

void KR(char *x, int m, char *y, int n) {
int d, hx, hy, i, j;

/* Preprocessing */
/* computes d = 2^(m-1) with
the left-shift operator */
for (d = i = 1; i < m; ++i)
d = (d<<1);

for (hy = hx = i = 0; i < m; ++i) {
hx = ((hx<<1) + x[i]);
hy = ((hy<<1) + y[i]);
}

/* Searching */
j = 0;
while (j <= n-m) {
if (hx == hy && memcmp(x, y + j, m) == 0)
OUTPUT(j);
hy = REHASH(y[j], y[j + m], hy);
++j;
}

}
Report
Re: C to VB Posted by BitByBit_Thor on 28 Feb 2005 at 9:27 AM
: Hi,
:
: Is there anyone here who could translate this C Code into VB? Or is it possible to link VB and C/C++ codes using the MSVisual Studio?? Thanks!
:
: #define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
:
: void KR(char *x, int m, char *y, int n) {
: int d, hx, hy, i, j;
:
: /* Preprocessing */
: /* computes d = 2^(m-1) with
: the left-shift operator */
: for (d = i = 1; i < m; ++i)
: d = (d<<1);
:
: for (hy = hx = i = 0; i < m; ++i) {
: hx = ((hx<<1) + x[i]);
: hy = ((hy<<1) + y[i]);
: }
:
: /* Searching */
: j = 0;
: while (j <= n-m) {
: if (hx == hy && memcmp(x, y + j, m) == 0)
: OUTPUT(j);
: hy = REHASH(y[j], y[j + m], hy);
: ++j;
: }
:
: }
:

Well... here it goes:
'Function Rehash (note: d has to be supplied as well!)
Function Rehash(a As Integer, b As Integer, h As Integer, _
  d As Integer) As Integer
  Rehash = (h - a * d) * 2 + b
End Function

Sub KR(ByRef x() As Byte, ByVal m As Integer, ByRef y() As Byte, _
  ByVal n As Integer)
  '% stands for As Integer
  Dim d%, hx%, hy%, i%, j As Integer

  ' /* Preprocessing */
  ' /* computes d = 2^(m-1) with
  'the left-shift operator */
  d = 2 ^ (m - 1)   
  i = m - 1
  hx = i
  hy = hx
  For i = 0 To m - 1
    hx = hx * 2 + x(i)
    hy = hy * 2 + y(i)
  Next

  '/* Searching */
  j = 0
  Do While (j <= n-m)
     'I am not really sure if I got this one right... Sure hope so
     If (hx = hy And CompareArrays(x, y, j, m)) Then
        'OUTPUT(j); '//Don't know what this does
     End If
     hy = Rehash(CInt(y(j)), CInt(y(j + m)), hy, d)
     j = j + 1
  Loop
End Sub

Function CompareArrays(x() As Byte, y() As Byte, j As Integer, _
m As Integer) As Boolean
  Dim i As Integer
  Dim bSame As Boolean
  
  bSame = True
  For i = j To m - 1
    If (x(i) <> y(i)) Then
      bSame = False
      Exit For
    End If
  Next i

  CompareArrays = bSame
End Function

I think this is correct, though I am not able to test it because I don't know what paramaters to supply to the function.
Greets...
Richard

Report
Re: C to VB Posted by Genjuro on 1 Mar 2005 at 9:24 AM
: : Hi,
: :
: : Is there anyone here who could translate this C Code into VB? Or is it possible to link VB and C/C++ codes using the MSVisual Studio?? Thanks!
: :
: : #define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
: :
: : void KR(char *x, int m, char *y, int n) {
: : int d, hx, hy, i, j;
: :
: : /* Preprocessing */
: : /* computes d = 2^(m-1) with
: : the left-shift operator */
: : for (d = i = 1; i < m; ++i)
: : d = (d<<1);
: :
: : for (hy = hx = i = 0; i < m; ++i) {
: : hx = ((hx<<1) + x[i]);
: : hy = ((hy<<1) + y[i]);
: : }
: :
: : /* Searching */
: : j = 0;
: : while (j <= n-m) {
: : if (hx == hy && memcmp(x, y + j, m) == 0)
: : OUTPUT(j);
: : hy = REHASH(y[j], y[j + m], hy);
: : ++j;
: : }
: :
: : }
: :
:
: Well... here it goes:
:
: 'Function Rehash (note: d has to be supplied as well!)
: Function Rehash(a As Integer, b As Integer, h As Integer, _
:   d As Integer) As Integer
:   Rehash = (h - a * d) * 2 + b
: End Function
: 
: Sub KR(ByRef x() As Byte, ByVal m As Integer, ByRef y() As Byte, _
:   ByVal n As Integer)
:   '% stands for As Integer
:   Dim d%, hx%, hy%, i%, j As Integer
: 
:   ' /* Preprocessing */
:   ' /* computes d = 2^(m-1) with
:   'the left-shift operator */
:   d = 2 ^ (m - 1)   
:   i = m - 1
:   hx = i
:   hy = hx
:   For i = 0 To m - 1
:     hx = hx * 2 + x(i)
:     hy = hy * 2 + y(i)
:   Next
: 
:   '/* Searching */
:   j = 0
:   Do While (j <= n-m)
:      'I am not really sure if I got this one right... Sure hope so
:      If (hx = hy And CompareArrays(x, y, j, m)) Then
:         'OUTPUT(j); '//Don't know what this does
:      End If
:      hy = Rehash(CInt(y(j)), CInt(y(j + m)), hy, d)
:      j = j + 1
:   Loop
: End Sub
: 
: Function CompareArrays(x() As Byte, y() As Byte, j As Integer, _
: m As Integer) As Boolean
:   Dim i As Integer
:   Dim bSame As Boolean
:   
:   bSame = True
:   For i = j To m - 1
:     If (x(i) <> y(i)) Then
:       bSame = False
:       Exit For
:     End If
:   Next i
: 
:   CompareArrays = bSame
: End Function
: 

: I think this is correct, though I am not able to test it because I don't know what paramaters to supply to the function.
: Greets...
: Richard

Just a quick note:

memcmp(x, y + j, m)

means basically "compare the m bytes starting from the address x with the same number of bytes starting from the address y + j". In other words, given that x and y are 0-based arrays of bytes, so the loop for CompareArrays should be:

for i = 0 to m - 1
     If (x(i) <> y(i + j)) Then
       bSame = False
       Exit For
     End If
next i 


It would be *very* wise to pass any parameter other than pointers in the original C code (j and m in the CompareArrays function) as ByVal, just in case (C has no idea of what ByVal and ByRef mean - it calls them respectively "copying the data", or "copying the pointer").
Also, I'd declare them as Longs - not that it probably matters,however.
Report
Re: C to VB Posted by brutallica on 1 Mar 2005 at 10:56 AM
: : : Hi,
: : :
: : : Is there anyone here who could translate this C Code into VB? Or is it possible to link VB and C/C++ codes using the MSVisual Studio?? Thanks!
: : :
: : : #define REHASH(a, b, h) ((((h) - (a)*d) << 1) + (b))
: : :
: : : void KR(char *x, int m, char *y, int n) {
: : : int d, hx, hy, i, j;
: : :
: : : /* Preprocessing */
: : : /* computes d = 2^(m-1) with
: : : the left-shift operator */
: : : for (d = i = 1; i < m; ++i)
: : : d = (d<<1);
: : :
: : : for (hy = hx = i = 0; i < m; ++i) {
: : : hx = ((hx<<1) + x[i]);
: : : hy = ((hy<<1) + y[i]);
: : : }
: : :
: : : /* Searching */
: : : j = 0;
: : : while (j <= n-m) {
: : : if (hx == hy && memcmp(x, y + j, m) == 0)
: : : OUTPUT(j);
: : : hy = REHASH(y[j], y[j + m], hy);
: : : ++j;
: : : }
: : :
: : : }
: : :
: :
: : Well... here it goes:
: :
: : 'Function Rehash (note: d has to be supplied as well!)
: : Function Rehash(a As Integer, b As Integer, h As Integer, _
: :   d As Integer) As Integer
: :   Rehash = (h - a * d) * 2 + b
: : End Function
: : 
: : Sub KR(ByRef x() As Byte, ByVal m As Integer, ByRef y() As Byte, _
: :   ByVal n As Integer)
: :   '% stands for As Integer
: :   Dim d%, hx%, hy%, i%, j As Integer
: : 
: :   ' /* Preprocessing */
: :   ' /* computes d = 2^(m-1) with
: :   'the left-shift operator */
: :   d = 2 ^ (m - 1)   
: :   i = m - 1
: :   hx = i
: :   hy = hx
: :   For i = 0 To m - 1
: :     hx = hx * 2 + x(i)
: :     hy = hy * 2 + y(i)
: :   Next
: : 
: :   '/* Searching */
: :   j = 0
: :   Do While (j <= n-m)
: :      'I am not really sure if I got this one right... Sure hope so
: :      If (hx = hy And CompareArrays(x, y, j, m)) Then
: :         'OUTPUT(j); '//Don't know what this does
: :      End If
: :      hy = Rehash(CInt(y(j)), CInt(y(j + m)), hy, d)
: :      j = j + 1
: :   Loop
: : End Sub
: : 
: : Function CompareArrays(x() As Byte, y() As Byte, j As Integer, _
: : m As Integer) As Boolean
: :   Dim i As Integer
: :   Dim bSame As Boolean
: :   
: :   bSame = True
: :   For i = j To m - 1
: :     If (x(i) <> y(i)) Then
: :       bSame = False
: :       Exit For
: :     End If
: :   Next i
: : 
: :   CompareArrays = bSame
: : End Function
: : 

: : I think this is correct, though I am not able to test it because I don't know what paramaters to supply to the function.
: : Greets...
: : Richard
:
: Just a quick note:
:
: memcmp(x, y + j, m)
:
: means basically "compare the m bytes starting from the address x with the same number of bytes starting from the address y + j". In other words, given that x and y are 0-based arrays of bytes, so the loop for CompareArrays should be:
:
:
: for i = 0 to m - 1
:      If (x(i) <> y(i + j)) Then
:        bSame = False
:        Exit For
:      End If
: next i 
: 

:
: It would be *very* wise to pass any parameter other than pointers in the original C code (j and m in the CompareArrays function) as ByVal, just in case (C has no idea of what ByVal and ByRef mean - it calls them respectively "copying the data", or "copying the pointer").
: Also, I'd declare them as Longs - not that it probably matters,however.
:
Thanks so much for your replies :) I'll try it out!!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.