Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to decode this in visual basic ?

Posted on 2005-03-12
10
Medium Priority
?
677 Views
Last Modified: 2013-11-25
http://www.experts-exchange.com/Programming/Programming_Languages/Q_21225628.html

I saw this and I am just curious, how would you go abouts decoding this in visual basic ??
0
Comment
Question by:gecko_au2003
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13527584
Translating the C code, I think it is the folowing. I have not tested since I have not VB in this machine, please advice:

str = "01001001001000000111001101110000011001010110111001110100001000000010010000110001001100000011000000100000011000010111010000100000010101000110100001101001011011100110101101000111011001010110010101101011001000000110000101101110011001000010000001100001011011000110110000100000010010010010000001100111011011110111010000100000011101110110000101110011001000000111010001101000011010010111001100100000011011000110111101110101011100110111100100100000011011000110100101101101011010010111010001100101011001000010000001100101011001000110100101110100011010010110111101101110001000000111010000101101011100110110100001101001011100100111010000101110"

strMessage=""
count=0
b=0
l = len(str)

for i=l to 1 step -1
     b = val(mid(str,i,1))+b*2
     count = count + 1
     if count = 8 then
          strMessage = chr(b) & strMessage
          b = 0
          count = 0
     end if
next
MsgBox(strMessage);
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 600 total points
ID: 13527606
Option Explicit

Private Sub Command1_Click()
    Dim str As String
    Dim strMessage As String
    Dim byteValue As Integer
    Dim i As Integer
    Dim j As Integer
    Dim strByte As String
   
    str = "01001001001000000111001101110000011001010110111001110100001000000010010000110001001100000011000000100000011000010111010000100000010101000110100001101001011011100110101101000111011001010110010101101011001000000110000101101110011001000010000001100001011011000110110000100000010010010010000001100111011011110111010000100000011101110110000101110011001000000111010001101000011010010111001100100000011011000110111101110101011100110111100100100000011011000110100101101101011010010111010001100101011001000010000001100101011001000110100101110100011010010110111101101110001000000111010000101101011100110110100001101001011100100111010000101110"
    For i = 1 To Len(str) Step 8
        byteValue = 0
        strByte = Mid(str, i, 8)
        For j = 8 To 1 Step -1
            byteValue = byteValue + Val(Mid(strByte, j, 1)) * (2 ^ (8 - j))
        Next j
        strMessage = strMessage & Chr(byteValue)
    Next
    MsgBox strMessage
End Sub
0
 
LVL 5

Accepted Solution

by:
Rubyn earned 800 total points
ID: 13527614
'Binary to Decimal Conversion
Private Function GetDec(Bin As String) As Long
Dim I As Integer
    For I = Len(Bin) To 1 Step -1
        Let GetDec = GetDec + Mid(Bin, I, 1) * (2 ^ (8 - I))
    Next
End Function

Private Sub Form_Load()
Dim BinStr As String, I As Integer, Msg As String
    Let BinStr = "01001001001000000111001101110000011001010110111001110100001000000010010000110001001100000011000000100000011000010111010000100000010101000110100001101001011011100110101101000111011001010110010101101011001000000110000101101110011001000010000001100001011011000110110000100000010010010010000001100111011011110111010000100000011101110110000101110011001000000111010001101000011010010111001100100000011011000110111101110101011100110111100100100000011011000110100101101101011010010111010001100101011001000010000001100101011001000110100101110100011010010110111101101110001000000111010000101101011100110110100001101001011100100111010000101110"
    Let I = Len(BinStr) + 1
    Do While (I > 8)
        Let I = I - 8
        'Get 8 bin segment from BinStr and calculate ascii character
        Let Msg = Chr(GetDec(Mid(BinStr, I, 8))) & Msg
    Loop
    Call MsgBox(Msg)
End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 600 total points
ID: 13529459
Here is a short code... with error handling

Private Sub Form_Load()
    Dim s As String
    Dim a As String
    s = "01001001001000000111001101110000011001010110111001110100001000000010010000110001001100000011000000100000011000010111010000100000010101000110100001101001011011100110101101000111011001010110010101101011001000000110000101101110011001000010000001100001011011000110110000100000010010010010000001100111011011110111010000100000011101110110000101110011001000000111010001101000011010010111001100100000011011000110111101110101011100110111100100100000011011000110100101101101011010010111010001100101011001000010000001100101011001000110100101110100011010010110111101101110001000000111010000101101011100110110100001101001011100100111010000101110"
    For i = 1 To (8 - Len(s) Mod 8)
        s = s & "0"
    Next

    While s <> ""
        a = a & Chr(ToDec(Left(s, 8)))
        s = Right(s, Len(s) - 8)
    Wend
    MsgBox a
End Sub

Private Function ToDec(ByVal bin As String) As Long
    For i = 1 To Len(bin)
        ToDec = ToDec + Mid(bin, i, 1) * (2 ^ (8 - i))
    Next
End Function
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13529719
I will split points with mgh_mghharish, idle_mind and Rubyn. However before I do If someone could please explain how the code works or comment each line very well so that it explains it as you go through the coding so that I understand how it works. That would be very much appreciated!

Thanks and kind regards

Gecko
0
 
LVL 5

Expert Comment

by:Rubyn
ID: 13529848
I think you know the concept. we are just reading 8 bits (acsii) and converting to decimal value and geting a char equvalent.
The reason why we are reading it from the last is because we dont know whether the first 8 bit data is of 8 bits or left most 0's are ignored.

'Binary to Decimal Conversion
Private Function GetDec(Bin As String) As Long
Dim I As Integer
    'This is to create a loop from 8 to 1, I used Len(Bin) instead of 8, because even if the input is 100 or 00000100 is 4.
    For I = Len(Bin) To 1 Step -1
        '100 : (0 x 2^0) + (0 x 2^1) + (1x2^2) = 4
        Let GetDec = GetDec + Mid(Bin, I, 1) * (2 ^ (8 - I))
    Next
End Function

Private Sub Form_Load()
Dim BinStr As String, I As Integer, Msg As String
    Let BinStr = "01001001001000000111001101110000011001010110111001110100001000000010010000110001001100000011000000100000011000010111010000100000010101000110100001101001011011100110101101000111011001010110010101101011001000000110000101101110011001000010000001100001011011000110110000100000010010010010000001100111011011110111010000100000011101110110000101110011001000000111010001101000011010010111001100100000011011000110111101110101011100110111100100100000011011000110100101101101011010010111010001100101011001000010000001100101011001000110100101110100011010010110111101101110001000000111010000101101011100110110100001101001011100100111010000101110"

    'Find the length of BinStr and adding 1 because I-8 should point the last start of the 8 bit segment.
    Let I = Len(BinStr) + 1

    'Check whether the I value should not go negative if the loop is allowed.
    Do While (I > 8)

        'Set the I value to point the start of the 8 bit segment position.
        Let I = I - 8

        'Get 8 bin segment from BinStr(I points the start and 8 represents the length) and calculate ascii character by calling GetDec function above.
        'Then append the found character to the left of the Msg
        Let Msg = Chr(GetDec(Mid(BinStr, I, 8))) & Msg

    Loop
    'Display the decoded String
    Call MsgBox(Msg)
End Sub
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13534992
gecko_au2003,

There may be more efficient ways to do this conversion from a string of "0" and "1"
characters represnein the binary value of ASCII characters into the ASCII string.   Than
these (VB) translations of the C++ code.

If you need to do a lot of this and performance is critical, you might consider asking
another question in the VB forum for performance improvements over the selected
answer from this discussion thread.
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13535028
Just wanted to say thanks to all who made comments and suggestions in here, I tried the first piece of code but there were errors in it.

aikimark - I will do this in the near future and thanks for that recomendation :) !!
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13535268
gecko_au2003,

You are welcome.  It might help if you added a comment to this thread and
included the URL to the follow-up performance question you create.
0
 
LVL 23

Author Comment

by:gecko_au2003
ID: 13536108
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

564 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