dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: How can i do this?

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    How can i do this?

    what can i do to get something similiar to this in vb

    unsigned long GetString(char *buffer, unsigned long bufferlen, char *string, unsigned long start) {
    unsigned long i, stringlen;
    stringlen = strlen(string);
    for (i=start; i<bufferlen-stringlen; i++) {
    if (memcmp(&buffer[i], string, stringlen) == 0)
    return i;
    }
    return 0;
    }

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Sure in VB just use the InStr() function like this:
    i = InStr(start, buf, str)

    This will return the position of str within buf or Zero (0) if it is not found.
    start is simply where within buf to start looking for str.

    If you want to start from the beginning of buf just leave start off like this:
    i = InStr(buf, str)

    FYI: 1 is always the start of buf not 0. In c++ everything starts at zero.
    Last edited by Ron Weller; 08-20-2009 at 05:21 AM.

  3. #3
    Join Date
    Aug 2009
    Posts
    2
    can you show a full translaiton please i tried using the line u posted but i dont get the same result

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Of course it works.
    But remember that the beginning of a c++ string is zero and the beginning of a vb string is 1. So InStr() will always return one more than the c++ routine. Now this may seem incorrect, but it isn't if you think about it. Each routine is returning an index, or location, of a string within another string.

    Example:
    buf = "this is a test"
    str = "is"
    start = 0


    c++ routine
    getstring(buf, 14, str, start)
    returns: 2

    vb routine
    InStr(buf, str)
    returns: 3

    Now set start to the 4th character, In the c++ string that is 3; but in the vb string it would be 4
    start = 3

    c++ routine
    getstring(buf, 14, str, start)
    returns: 5

    vb routine
    add 1 to start vb at the 4th character like the c++ routine
    InStr(start + 1, buf, str)
    returns: 6

    Now change str to "this" and start to zero
    str = "this"
    start = 0

    c++ routine
    getstring(buf, 14, str, start)
    returns: 0

    vb routine
    InStr(buf, str)
    returns: 1

    Notice the flaw in the c++ routine. It returns zero whenever it finds a match at the beginning of the buffer string. But it also returns zero when it does not find a match. This could be confusing depending on how you use the result.
    Last edited by Ron Weller; 08-20-2009 at 07:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center