Results 1 to 9 of 9

Thread: Help convert a string modification code from C++ to VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    4

    Help convert a string modification code from C++ to VB

    Hi all.

    I have written a C++ code to modify a string, the code is:

    Code:
    int Len = (nString.length());   
    char *szString = (char*)(nString.c_str()); // Convert to C string 
    	
    for (int i = 0; i < Len; i++)        
    {
     *(szString+i) = (*(szString+i) + 123);     //XOR
    }
    Can someone help me convert this to VB:

    I have this so far, but it is not working:
    Code:
            Dim i As Integer
            Dim chrArray() As Char
    
            chrArray = EncryptIn.ToCharArray
    
            For intCounter = 0 To chrArray.Length - 1
                chrArray(i) = chrArray(i + 123)
             Next
    Thanks bunch!

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Help convert a string modification code from C++ to VB

    What does "it is not working" mean exactly? Do you get an error? Does something occur that you're not expecting? What are you expecting?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Help convert a string modification code from C++ to VB

    The line within the loop should be:
    Code:
    chrArray(i) = chrArray(i) + 123
    Nah - that doesn't compile - I have no idea what you're trying to do with that original C++ code - could you explain?
    Last edited by David Anton; Jan 23rd, 2011 at 05:46 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Help convert a string modification code from C++ to VB

    Does this work?? Or does VB throw an overflow??
    Code:
    charArray(i) += ChrW(123)
    In C# you could use this
    Code:
    charArray[i] += (char)123;
    And I think the C++ could be rewritten as
    Code:
    szString[i] += 123;
    W o t . S i g

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Help convert a string modification code from C++ to VB

    I think the loop line should read:

    Code:
    chrArray(i) = Chr(Asc(chrArray(i)) Xor 123)
    ?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    4

    Re: Help convert a string modification code from C++ to VB

    Quote Originally Posted by David Anton View Post
    The line within the loop should be:
    Code:
    chrArray(i) = chrArray(i) + 123
    Nah - that doesn't compile - I have no idea what you're trying to do with that original C++ code - could you explain?

    Quote Originally Posted by weirddemon View Post
    What does "it is not working" mean exactly? Do you get an error? Does something occur that you're not expecting? What are you expecting?

    Thanks for helping guys, what I expect is to do some simple mathematical operation to the char bytes to encrypt the string

    for example:
    If I have string = "abc", and when I do this (in C++)

    Code:
    	for (int i = 0; i < Len; i++)   
    	{ *(szString+i) = (*(szString+i)+1);	}
    The string will become "bcd", so obviously for the real code I will have much more complicated formula to generate the new string

    I hope this makes sense now, basically is the ability to do char # of each character one by one in the string
    Last edited by boardgg; Jan 23rd, 2011 at 09:44 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    4

    Re: Help convert a string modification code from C++ to VB

    Quote Originally Posted by Evil_Giraffe View Post
    I think the loop line should read:

    Code:
    chrArray(i) = Chr(Asc(chrArray(i)) Xor 123)
    ?
    Hello.

    I tested with
    Code:
    chrArray(i) = Chr(Asc(chrArray(i)) + 1)
    and that is what I wanted, however when I tried to do more complicated calculation, such as Chr(Asc(chrArray(i)) Xor 123456) I got error of "Procedure call or argument is not valid"

    Any idea why? Seems the number is outside of Chr()'s range (max 65536)? I actually do want to xor with a huge number, in c++ it works.
    thanks!
    Last edited by boardgg; Jan 23rd, 2011 at 11:07 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    4

    Re: Help convert a string modification code from C++ to VB

    Sorry about bumping this myself, really in need of a solution to this
    basically is how to get chr() of value greater than 65536?

    thanks!

  9. #9
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Help convert a string modification code from C++ to VB

    Basically you can't, and you can't in C++ either.

    Your question could be reworded as 'How can I fit more than 2 bytes into 2 bytes'

    C++ allows you to overflow numbers silently, VB.net doesn't.

    If you want to Xor a char with a large number try using a bitwise And &hFFFF with it first to truncate it.
    W o t . S i g

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width