Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I write this Java code in VB??

Posted on 2001-06-09
8
Medium Priority
?
145 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
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!

 
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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

670 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