?
Solved

Break Binary String into Blocks

Posted on 2001-07-26
11
Medium Priority
?
268 Views
Last Modified: 2008-03-03
VB6 Pro.......Win98 2nd Ed.

 I have a binary string i.e.
11010101111001
 Would like to break up into block of 4 i.e.
11-0101-0111-1001

0
Comment
Question by:et1dkn
[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
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6324503
Here you go.

Zaphod.


Dim intPosition as integer
Dim strNewBinary as string
intPosition = len(strBinary)
If len(strBinary) mod 4 <> 0 then
   strNewBinary = left(strBinary, len(strBinary) mod 4) & "-"
End if
For intPosition = len(strBinary) mod 4 to len(strBinary)
    strNewBinary = strNewBinary & mid(strBinary, intPosition,4) & "-"
Next
strBinary = left(strBinary,len(strBinary)-1)
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6324509
Oh, strBinary contains the original binary string, and this will fail if the string is empty, so you might want to add a check at the start.

Zaphod.
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6324519
Doh, small error:

Dim intPosition as integer
Dim strNewBinary as string
intPosition = len(strBinary)
If len(strBinary) mod 4 <> 0 then
  strNewBinary = left(strBinary, len(strBinary) mod 4) & "-"
End if
For intPosition = (len(strBinary) mod 4)+1 to len(strBinary)
   strNewBinary = strNewBinary & mid(strBinary, intPosition,4) & "-"
Next
strBinary = left(strBinary,len(strBinary)-1)
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!

 

Author Comment

by:et1dkn
ID: 6324741
Z B..........
Your routine returns the original string...
that is no "-" inserted in string.  Believe you have
some conflict with strBinary and strNewBinary.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6324979
I was going to suggest using a masked edit box with the mask = "####-####-####-####" but I can't figure out how to right-justify without putting in a lot of code, in which case, just do it by hand:

Dim binstring As String
Dim z As Integer
binstring = "11010101111001"
For z = Len(binstring) - 3 To 1 Step -4
  binstring = Left$(binstring, z - 1) & "-" & Mid$(binstring, z)
Next z
MsgBox binstring
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6324988
I guess this is similar to the Z_Beeblebrox comment, but by working backwords, you don't have to worry about the expanding size of the result and can therefore keep it in the same variable, if that's what you want.
0
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 400 total points
ID: 6325032
Dim intPosition As Integer
Dim strNewBinary As String

intPosition = Len(strBinary)
If Len(strBinary) Mod 4 <> 0 Then
 strNewBinary = Left(strBinary, Len(strBinary) Mod 4) & "-"
End If
For intPosition = (Len(strBinary) Mod 4) + 1 To Len(strBinary) Step 4
  strNewBinary = strNewBinary & Mid(strBinary, intPosition, 4) & "-"
Next
strNewBinary = Left(strNewBinary, Len(strNewBinary) - 1)

I made a couple mistakes, I guess this just isn't my day. Anyways, that should do it, I actually tested it this time.

Zaphod.
0
 
LVL 1

Expert Comment

by:ramani_gr
ID: 6325033
Try this.

    Dim intPosition As Integer
    Dim strNewBinary As String
    Dim strbinary  As String
   
    strbinary = "11010101111001"
    For intPosition = Len(strbinary) To 0 Step -4
       If intPosition - 3 > 0 Then
            strNewBinary = Mid(strbinary, intPosition - 3, 4) & IIf(strNewBinary = "", "", "-") & strNewBinary
        Else
            strNewBinary = Mid(strbinary, 1, intPosition) & IIf(strNewBinary = "", "", "-") & strNewBinary
        End If
    Next
    MsgBox strNewBinary
    '11-0101-0111-1001

this could slove ur problem.

0
 
LVL 28

Expert Comment

by:Ark
ID: 6325090
  s = "11010101111001"
   Debug.Print Format(s, "&&&&-&&&&-&&&&-&&&&")
Cheers
0
 
LVL 28

Expert Comment

by:Ark
ID: 6325104
PS. If you want to split output string:

s = "11010101111001"
sBin = Format(s, "&&&&-&&&&-&&&&-&&&&")
and then use Split function.

or another solution

Private Type FullString
   str As String * 16
End Type

Private Type PartString
   First As String * 4
   Second As String * 4
   Third As String * 4
   Fourth As String * 4
End Type

Private Sub Command1_Click()
   Dim fs As FullString
   Dim ps As PartString
   RSet fs.str = "11010101111001"
   LSet ps = fs
   Debug.Print ps.First, ps.Second, ps.Third, ps.Fourth
End Sub



0
 

Author Comment

by:et1dkn
ID: 6325401
Z B
Thanks much for the prompt reply.........
Dan
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

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