Solved

How do I write this Java code in VB??

Posted on 2001-06-09
8
137 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

14 Experts available now in Live!

Get 1:1 Help Now