Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How to convert to binary string on micro-framework


Best Answer baxter, 31 May 2013 - 04:13 AM

Dim reply As String = "BE3EA813"Dim int As Integer = Convert.ToInt32(reply, 16)Dim Bin As String = ToBinary(CUInt(int))Debug.Print("Bin: " & Bin) your answer     = 10111110001111101010100000010011ToBinary answer = 10111110001111101010100000010011Public Function ToBinary(ByVal x As UInt32) As String        'Convert an unsigned integer to a binary string        Dim temp As String = ""        Do            If CBool(x Mod 2) Then                temp = "1" + temp            Else                temp = "0" + temp            End If            x = CUInt(x  2)            If x < 1 Then Exit Do        Loop        Return temp    End Function

EDIT:

Dim MyInt As UInt32 = BinToInt(Bin)Debug.Print("MyInt: " & MyInt.ToString)MyInt: 3191777299, Windows calculator --> BE3EA813Public Function BinToInt(bin As String) As UInteger        'Horner's method to evaluate a polynomial        '01110110 msb, msb-1, ... 0 (e.g. msb to lsb, left to right)        Dim len As Integer = bin.Length        Dim temp As UInteger = CUInt(If(bin.Chars(0) = "0", 0, 1))        For i As Integer = 1 To len - 1            temp = CUInt(temp * 2 + If(bin.Chars(i) = "0", 0, 1))        Next        Return tempEnd Function
Go to the full post


  • Please log in to reply
3 replies to this topic

#1 Joshk

Joshk

    Advanced Member

  • Members
  • PipPipPip
  • 72 posts

Posted 31 May 2013 - 01:44 AM

I have the following code working perfectly on a vb.net desktop app, but I can not figure out the equivalent code for vb.net micro-framework.

 

      Dim reply As String = "BE3EA813"

        Dim int As Integer = Convert.ToInt32(reply, 16)
        Dim binString As String = Convert.ToString(int, 2)
        MsgBox(binString) 'Correctly says "10111110001111101010100000010011"

 

The Convert.ToInt32(reply, 16) is available and works, but the Convert.ToString(int, 2) is not available!



#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 31 May 2013 - 04:13 AM   Best Answer

Dim reply As String = "BE3EA813"Dim int As Integer = Convert.ToInt32(reply, 16)Dim Bin As String = ToBinary(CUInt(int))Debug.Print("Bin: " & Bin) your answer     = 10111110001111101010100000010011ToBinary answer = 10111110001111101010100000010011Public Function ToBinary(ByVal x As UInt32) As String        'Convert an unsigned integer to a binary string        Dim temp As String = ""        Do            If CBool(x Mod 2) Then                temp = "1" + temp            Else                temp = "0" + temp            End If            x = CUInt(x  2)            If x < 1 Then Exit Do        Loop        Return temp    End Function

EDIT:

Dim MyInt As UInt32 = BinToInt(Bin)Debug.Print("MyInt: " & MyInt.ToString)MyInt: 3191777299, Windows calculator --> BE3EA813Public Function BinToInt(bin As String) As UInteger        'Horner's method to evaluate a polynomial        '01110110 msb, msb-1, ... 0 (e.g. msb to lsb, left to right)        Dim len As Integer = bin.Length        Dim temp As UInteger = CUInt(If(bin.Chars(0) = "0", 0, 1))        For i As Integer = 1 To len - 1            temp = CUInt(temp * 2 + If(bin.Chars(i) = "0", 0, 1))        Next        Return tempEnd Function


#3 Joshk

Joshk

    Advanced Member

  • Members
  • PipPipPip
  • 72 posts

Posted 01 June 2013 - 06:52 PM

Awesome baxter, you rock!  I would have never figured that out and Google let me down on this one.



#4 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 01 June 2013 - 08:42 PM

Glad to help ... Incidentally, if you are looking for stuff like this on Google, search like this,

 

<your search criteria> site:edu






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.