Solved

How do I write this Java code in VB??

Posted on 2001-06-09
8
141 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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