Solved

How do I write this Java code in VB??

Posted on 2001-06-09
8
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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!

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

752 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