BitConverter.ToUInt64 Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george r smith

    BitConverter.ToUInt64 Question

    Hi,

    I created a byte array
    --byte[] aByteArray = new byte[64];
    and set aByteArray[0] and aByteArray[16] to '1'.

    Because I want to create a ulong (UInt64) with these values I do:
    UInt64 bb;
    bb = BitConverter.To UInt64(aByteArr ay,0);
    Console.WriteLi ne(bb);

    I get a 1. So I am only getting the first eight bytes of aByteArray. This is
    what
    the docs say. So how can I get these values into a ulong and back again into
    an array.

    Why would they have BitConverter only deal with 8 bytes. I am trying hard to
    understand
    BitArray class, the BitConverter class and the interplay between them.

    thanks
    grs


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: BitConverter.To UInt64 Question

    George,

    This behavior is correct. Since a byte is 8 bits, and an Int64 is 64
    bits, you need 8 bytes to get the representation of an Int64 (or ulong).

    Now, if you are using a BitArray, then you are actually working with the
    individual bits, not bytes, and you will need 64 entries to get an Int64
    from that class.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "george r smith" <gsmith@budgete xt.com> wrote in message
    news:O1IoptqsDH A.536@tk2msftng p13.phx.gbl...[color=blue]
    > Hi,
    >
    > I created a byte array
    > --byte[] aByteArray = new byte[64];
    > and set aByteArray[0] and aByteArray[16] to '1'.
    >
    > Because I want to create a ulong (UInt64) with these values I do:
    > UInt64 bb;
    > bb = BitConverter.To UInt64(aByteArr ay,0);
    > Console.WriteLi ne(bb);
    >
    > I get a 1. So I am only getting the first eight bytes of aByteArray. This[/color]
    is[color=blue]
    > what
    > the docs say. So how can I get these values into a ulong and back again[/color]
    into[color=blue]
    > an array.
    >
    > Why would they have BitConverter only deal with 8 bytes. I am trying hard[/color]
    to[color=blue]
    > understand
    > BitArray class, the BitConverter class and the interplay between them.
    >
    > thanks
    > grs
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: BitConverter.To UInt64 Question

      george r smith <gsmith@budgete xt.com> wrote:[color=blue]
      > I created a byte array
      > --byte[] aByteArray = new byte[64];
      > and set aByteArray[0] and aByteArray[16] to '1'.
      >
      > Because I want to create a ulong (UInt64) with these values I do:
      > UInt64 bb;
      > bb = BitConverter.To UInt64(aByteArr ay,0);
      > Console.WriteLi ne(bb);
      >
      > I get a 1. So I am only getting the first eight bytes of aByteArray. This is
      > what the docs say. So how can I get these values into a ulong and back again into
      > an array.[/color]

      How do you expect to get 64 bytes into a ulong, when a ulong is only 8
      bytes (64 *bits*) long?
      [color=blue]
      > Why would they have BitConverter only deal with 8 bytes.[/color]

      Because longs and ulongs are only 8 bytes long.
      [color=blue]
      > I am trying hard to understand
      > BitArray class, the BitConverter class and the interplay between them.[/color]

      I'm not sure where BitArray comes in here.

      Take a step back - what do you want to do, exactly?

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • george r smith

        #4
        Re: BitConverter.To UInt64 Question

        Jon,

        What I am trying to is two things.
        1. Create a chess bitboard of 64 bits stored as a ulong.
        2. Create an array (BitArray or regular array) of same size for ease of
        debugging and display.
        For this I want to capability of converting back and forth.

        Thanks.
        grs


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: BitConverter.To UInt64 Question

          george r smith <gsmith@budgete xt.com> wrote:[color=blue]
          > What I am trying to is two things.
          > 1. Create a chess bitboard of 64 bits stored as a ulong.
          > 2. Create an array (BitArray or regular array) of same size for ease of
          > debugging and display.
          > For this I want to capability of converting back and forth.[/color]

          I'd suggest you just abstract it to a class which only keeps it in a
          long, with properties which just manipulate the bits within the long.
          For the sake of debugging you could just write a method which converted
          it to a string representation. You don't usually need to change things
          during debugging, just examine them (IME).

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jeffrey Tan[MSFT]

            #6
            Re: BitConverter.To UInt64 Question


            Hi George,

            Unsigned long need 8 bytes(64bits) to store its value.
            I think you can first create a ulong variable. Then you do like below to
            convert this ulong into a bitarray:
            UInt64 bb=/*value*/;
            byte [] storearr=BitCon verter.GetBytes (bb);
            BitArray ba=new BitArray(storea rr);

            Then you can use System.Collecti ons.IEnumerator to enumerate every bits of
            the bitarray.
            For more information, please refer to:

            frlrfsystemcoll ectionsbitarray classtopic.asp

            Hope this helps,
            Best regards,
            Jeffrey Tan
            Microsoft Online Partner Support
            Get Secure! - www.microsoft.com/security
            This posting is provided "as is" with no warranties and confers no rights.

            Comment

            Working...