Results 1 to 6 of 6

Thread: How to turn an integer into a binary string?

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    How to turn an integer into a binary string?

    How can I WITHOUT USING THE VB COMPABILITY NAMESPACE get it's binary representation? For example : 3021 would turn into 101111001101

    tks
    \m/\m/

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Something like this, should do it.
    This uses a 16 bit value, hence the value of 15, but you can just as well use 31 instead of 15 to use a 32 bit value.

    VB Code:
    1. Dim S as String
    2. Dim I as Integer
    3.  
    4. For I=15 To 0 Step -1
    5.   If (Value And 2^I)=2^I Then
    6.     S &= "1"
    7.   Else
    8.     S &= "0"
    9.   End If
    10. Next
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I posted a function in this thread that converts signed long integers. Not sure what you'd need to do to modify it for .NET though.

    http://www.vbforums.com/showthread.p...hreadid=282094
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. Dim val() As Integer = {1}
    2.  
    3.         Dim x As New BitArray(val)
    4.         Dim strOut As String
    5.  
    6.         For i As Integer = 0 To x.Count - 1
    7.             strOut &= IIf(x.Item(i), "1", "0")
    8.         Next
    9.  
    10.         Debug.WriteLine(StrReverse(strOut))
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    PT Exorcist, is this related to your thread where 4635 = 18,27? If so, you don't need to go through all that trouble

    VB Code:
    1. Dim port As Integer = 4635
    2.         MessageBox.Show(Decimal.Truncate(port / 256))
    3.         MessageBox.Show(port Mod 256)

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    You rule so much Ill try it now.
    \m/\m/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width