Solved

How do I write this Java code in VB??

Posted on 2001-06-09
8
138 Views
Last Modified: 2010-05-02
I would like to rewrite the following Java code in vb code.

Public String decode(String s)
 {
    byte byte0 = 20
    s = s.trim();
    int j = s.length();
    char ac[] = s.toCharArray();
    for(int i = 0; i < j; i++)
    {
        int k = ac[i] - byte0;
        if(k < 32)
            ac[i] = (char) (95 + k);

        else
            ac[i] = (char)k;
    }

    String s1 = new String(ac);
    return s1;
}

0
Comment
Question by:jimmyjoe
8 Comments
 
LVL 2

Expert Comment

by:kmv
ID: 6170501
0
 
LVL 2

Expert Comment

by:kmv
ID: 6170511
Smth like next.

Public function decode(String s) As String
   Dim byte0, str
   Dim j, k
   byte0 = 20
   s = trim(s)
   for j = 0 To len(s)
       k = CInt(Mid(s, j, 1)) - byte0
       if k < 32 then
           str = str & CStr(95 + k)
       else
           str = str & CStr(k)
       end if
   Next
   decode = str
end function
0
 
LVL 2

Expert Comment

by:kmv
ID: 6170519
Change first string to
Public function decode(s As string) As String
0
 
LVL 2

Expert Comment

by:Microsoft
ID: 6171373
Public function decode(S AS String) As String
  Dim byte0, str
  Dim j, k
  byte0 = 20
  s = trim(s)
  for j = 0 To len(s)
      k = CInt(Mid(s, j, 1)) - byte0
      if k < 32 then
          str = str & CStr(95 + k)
      else
          str = str & CStr(k)
      end if
  Next
  decode = str
end function
0
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.

 

Author Comment

by:jimmyjoe
ID: 6171390
Invalid procedure on

k = Cint(Mid(s,j,1)) - byte0


0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6172266
interesting...
0
 
LVL 1

Accepted Solution

by:
HelixDaKat earned 200 total points
ID: 6172501
Converted, Tested, and Commented
I am also providing the ENCODE Function too. (This is how I tested it.)

'----------------------------------------------------------
Public Function Decode(strString As String) As String
 
   'Declare ALL Variables.
   Dim Key As Integer
   Dim iAscii As Integer
   Dim iPosition As Integer
   Dim strNewString As String
   Dim LengthOfString As Integer
   
   'Set the Key Value
   Key = 20
   
   'Trim the passed in string
   strString = Trim(strString)
   'Get the length of the string
   LengthOfString = Len(strString)
   
   'Look at each character in the string
   For iPosition = 1 To LengthOfString
       'Get the Ascii value of the character that is at iPosition and subtracting the key value
       iAscii = Asc(Mid$(strString, iPosition, 1)) - Key
       'Check if the Ascii code is less than a <Space> (32)
       If iAscii < 32 Then
           'If it is less than 32 then add 95 to it.
           strNewString = strNewString & Chr(iAscii + 95)
       Else
            'Else just use what we got from sutracting the Key value.
            strNewString = strNewString & Chr(iAscii)
        End If
    Next
   
    'Return the Newstring back to the calling function
    Decode = strNewString
   
End Function


'--------------------------------------------------------
Public Function Encode(strString As String) As String
 
   'Declare ALL Variables.
   Dim Key As Integer
   Dim iAscii As Integer
   Dim iPosition As Integer
   Dim strNewString As String
   Dim LengthOfString As Integer
   
   'Set the Key Value
   Key = 20
   
   'Trim the passed in string
   strString = Trim(strString)
   'Get the length of the string
   LengthOfString = Len(strString)
   
   'Look at each character in the string
   For iPosition = 1 To LengthOfString
       'Get the Ascii value of the character that is at iPosition, and add the key value
       iAscii = Asc(Mid$(strString, iPosition, 1)) + Key
       'Check if the Ascii code is less than a ~ (126)
       If iAscii > 126 Then
           'If it is less than 126 then subtract 95 from it.
           strNewString = strNewString & Chr(iAscii - 95)
       Else
            'Else just use what we got from adding the Key value.
            strNewString = strNewString & Chr(iAscii)
        End If
    Next
   
    'Return the Newstring back to the calling function
    Encode = strNewString
   
End Function



HelixDaKat
0
 
LVL 1

Expert Comment

by:HelixDaKat
ID: 6172502
It may look like a lot of code, but most of it is comments.
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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 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

13 Experts available now in Live!

Get 1:1 Help Now