Convert UInt16 to binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Convert UInt16 to binary

    Hi group,

    Is there an easy way to convert a UInt16 value in to a string which presents
    it in a binary format. I need a conversion with a fixed length.

    so 6 must be presented as '00000000000001 10'

    Any help or code examples would be higly appreciated.

    Thanks a lot in advance,

    Bart

  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Convert UInt16 to binary

    bart wrote:
    Is there an easy way to convert a UInt16 value in to a string which
    presents it in a binary format. I need a conversion with a fixed length.
    >
    so 6 must be presented as '00000000000001 10'
    >
    Any help or code examples would be higly appreciated.
    Try:

    Convert.ToStrin g(v, 2).PadLeft(16, '0')

    Arne

    Comment

    • Cowboy \(Gregory A. Beamer\)

      #3
      Re: Convert UInt16 to binary

      I agree with Arne, if you are trying to get a string representation? Are you
      trying to get a byte[2] instead?

      --
      Gregory A. Beamer
      MVP, MCP: +I, SE, SD, DBA

      Subscribe to my blog


      or just read it:


      *************** *************** **************
      | Think outside the box! |
      *************** *************** **************
      <bartwrote in message
      news:3EF110A7-E6F8-47B0-8E5C-837F082570A5@mi crosoft.com...
      Hi group,
      >
      Is there an easy way to convert a UInt16 value in to a string which
      presents it in a binary format. I need a conversion with a fixed length.
      >
      so 6 must be presented as '00000000000001 10'
      >
      Any help or code examples would be higly appreciated.
      >
      Thanks a lot in advance,
      >
      Bart

      Comment

      • Jeroen Mostert

        #4
        Re: Convert UInt16 to binary

        Arne Vajhøj wrote:
        bart wrote:
        >Is there an easy way to convert a UInt16 value in to a string which
        >presents it in a binary format. I need a conversion with a fixed length.
        >>
        >so 6 must be presented as '00000000000001 10'
        >>
        >Any help or code examples would be higly appreciated.
        >
        Try:
        >
        Convert.ToStrin g(v, 2).PadLeft(16, '0')
        >
        Boooo-ring. These may be more acceptable for the LINQ era:

        new string(Enumerab le.Range(0, 16).Reverse().S elect(i ="01"[v >i &
        1]).ToArray())

        Or

        new string(Enumerab le.Repeat(v, 16).Select((x, y) ="01"[x >y &
        1]).Reverse().ToA rray())

        Or

        Enumerable.Repe at(v, 16).Aggregate(" ", (x, y) ="01"[y >x.Length & 1] + x)

        (I'm kidding, don't use these.)

        --
        J.

        Comment

        • Guest's Avatar

          #5
          Re: Convert UInt16 to binary

          Aha...PadLef... ...so simple


          Thanks a lot :)

          "Arne Vajhøj" <arne@vajhoej.d kschreef in bericht
          news:490f9089$0 $90269$14726298 @news.sunsite.d k...
          bart wrote:
          >Is there an easy way to convert a UInt16 value in to a string which
          >presents it in a binary format. I need a conversion with a fixed length.
          >>
          >so 6 must be presented as '00000000000001 10'
          >>
          >Any help or code examples would be higly appreciated.
          >
          Try:
          >
          Convert.ToStrin g(v, 2).PadLeft(16, '0')
          >
          Arne

          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: Convert UInt16 to binary

            bart wrote:
            "Arne Vajhøj" <arne@vajhoej.d kschreef in bericht
            news:490f9089$0 $90269$14726298 @news.sunsite.d k...
            >bart wrote:
            >>Is there an easy way to convert a UInt16 value in to a string which
            >>presents it in a binary format. I need a conversion with a fixed length.
            >>>
            >>so 6 must be presented as '00000000000001 10'
            >>>
            >>Any help or code examples would be higly appreciated.
            >>
            >Try:
            >>
            >Convert.ToStri ng(v, 2).PadLeft(16, '0')
            Aha...PadLef... ...so simple
            Simple is good in programming !

            If you need to obfuscate the code, then Jeroen had
            some ideas.

            Arne

            Comment

            Working...