?
Solved

how to decode this in visual basic ?

Posted on 2005-03-12
10
Medium Priority
?
659 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
[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
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

764 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