Type Conversion Problem

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

    #1

    Type Conversion Problem

    Hi all,

    Need some help on a C# problem.
    Playing with chess stuff - great fun to learn with.

    int sq;
    sq = 0;
    UInt64 bb = 0;
    sq = 1;
    bb |= (1ul << sq);
    sq = 6;
    bb |= (1ul << sq);
    sq = 57;
    bb |= (1ul << sq);
    sq = 62;
    bb |= (1ul << sq);

    Console.WriteLi ne(bb); // this prints 4,755,801,206,5 03,243,842

    Now we have this ulong (UInt64) that has the following values in binary,hex
    and decimal:
    01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
    42 00 00 00 00 00 00 42
    4,755,801,206,5 03,243,842

    I know that it might not be the best way to go about chess programming but
    what I want to do is to convert this ulong bb into either a regular array
    using

    (byte[] aByteArray = new byte[64])
    or
    a BitArray using (BitArray aBitArray = new BitArray(64).


    Will someone please show me the way.
    Thanks
    grs


  • 100

    #2
    Re: Type Conversion Problem

    Hi george,
    Try to use BitConverter.Ge tBytes static method. It has overload for UInt64
    and it is what you need I believe.

    HTH
    B\rgds
    100
    "george r smith" <gsmith@budgete xt.com> wrote in message
    news:umAjJOFtDH A.1760@TK2MSFTN GP10.phx.gbl...
    [color=blue]
    > Hi all,
    >
    > Need some help on a C# problem.
    > Playing with chess stuff - great fun to learn with.
    >
    > int sq;
    > sq = 0;
    > UInt64 bb = 0;
    > sq = 1;
    > bb |= (1ul << sq);
    > sq = 6;
    > bb |= (1ul << sq);
    > sq = 57;
    > bb |= (1ul << sq);
    > sq = 62;
    > bb |= (1ul << sq);
    >
    > Console.WriteLi ne(bb); // this prints 4,755,801,206,5 03,243,842
    >
    > Now we have this ulong (UInt64) that has the following values in[/color]
    binary,hex[color=blue]
    > and decimal:
    > 01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
    > 42 00 00 00 00 00 00 42
    > 4,755,801,206,5 03,243,842
    >
    > I know that it might not be the best way to go about chess programming but
    > what I want to do is to convert this ulong bb into either a regular array
    > using
    >
    > (byte[] aByteArray = new byte[64])
    > or
    > a BitArray using (BitArray aBitArray = new BitArray(64).
    >
    >
    > Will someone please show me the way.
    > Thanks
    > grs
    >
    >[/color]


    Comment

    • george r smith

      #3
      Re: Type Conversion Problem

      thanks but I tried that - GetBytes returns an 8 byte array - I want either
      64 bits in a BitArray or an 64 byte array that will hold a 1 or 0 in it.

      george

      "100" <100@100.com> wrote in message
      news:eKZEiRFtDH A.628@tk2msftng p13.phx.gbl...[color=blue]
      > Hi george,
      > Try to use BitConverter.Ge tBytes static method. It has overload for UInt64
      > and it is what you need I believe.
      >
      > HTH
      > B\rgds
      > 100
      > "george r smith" <gsmith@budgete xt.com> wrote in message
      > news:umAjJOFtDH A.1760@TK2MSFTN GP10.phx.gbl...
      >[color=green]
      > > Hi all,
      > >
      > > Need some help on a C# problem.
      > > Playing with chess stuff - great fun to learn with.
      > >
      > > int sq;
      > > sq = 0;
      > > UInt64 bb = 0;
      > > sq = 1;
      > > bb |= (1ul << sq);
      > > sq = 6;
      > > bb |= (1ul << sq);
      > > sq = 57;
      > > bb |= (1ul << sq);
      > > sq = 62;
      > > bb |= (1ul << sq);
      > >
      > > Console.WriteLi ne(bb); // this prints 4,755,801,206,5 03,243,842
      > >
      > > Now we have this ulong (UInt64) that has the following values in[/color]
      > binary,hex[color=green]
      > > and decimal:
      > > 01000010 00000000 00000000 00000000 00000000 00000000 00000000 01000010
      > > 42 00 00 00 00 00 00 42
      > > 4,755,801,206,5 03,243,842
      > >
      > > I know that it might not be the best way to go about chess programming[/color][/color]
      but[color=blue][color=green]
      > > what I want to do is to convert this ulong bb into either a regular[/color][/color]
      array[color=blue][color=green]
      > > using
      > >
      > > (byte[] aByteArray = new byte[64])
      > > or
      > > a BitArray using (BitArray aBitArray = new BitArray(64).
      > >
      > >
      > > Will someone please show me the way.
      > > Thanks
      > > grs
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • 100

        #4
        Re: Type Conversion Problem

        Oh, sory george I must have read the question more carefully.
        So I don't know if there is ready for use solution in the framework.
        BitConverter and BitArray's constructors obviously won't do.
        So, I wrote it myself

        ui= 0x4200000000000 042ul;

        //UInt64 mask = 1ul;// for little-endian order
        byte[] byteArray = new byte[64];
        BitArray bitArray = new BitArray(64);

        //this is for index zero for the most significant bit;
        UInt64 mask = 1ul << 63;
        for(int i = 0; i < 64; i++)
        {
        byteArray[i] = (byte)(((ui & mask) != 0)?1:0);
        bitArray[i] = (ui & mask) != 0;
        mask >>= 1;
        }

        //To reverse the order reverse the for loop or move the mask the other
        way.

        HTH
        B\rgds
        100
        "george r smith" <gsmith@budgete xt.com> wrote in message
        news:ubXcRmFtDH A.1060@TK2MSFTN GP12.phx.gbl...[color=blue]
        > thanks but I tried that - GetBytes returns an 8 byte array - I want either
        > 64 bits in a BitArray or an 64 byte array that will hold a 1 or 0 in it.
        >
        > george
        >
        > "100" <100@100.com> wrote in message
        > news:eKZEiRFtDH A.628@tk2msftng p13.phx.gbl...[color=green]
        > > Hi george,
        > > Try to use BitConverter.Ge tBytes static method. It has overload for[/color][/color]
        UInt64[color=blue][color=green]
        > > and it is what you need I believe.
        > >
        > > HTH
        > > B\rgds
        > > 100
        > > "george r smith" <gsmith@budgete xt.com> wrote in message
        > > news:umAjJOFtDH A.1760@TK2MSFTN GP10.phx.gbl...
        > >[color=darkred]
        > > > Hi all,
        > > >
        > > > Need some help on a C# problem.
        > > > Playing with chess stuff - great fun to learn with.
        > > >
        > > > int sq;
        > > > sq = 0;
        > > > UInt64 bb = 0;
        > > > sq = 1;
        > > > bb |= (1ul << sq);
        > > > sq = 6;
        > > > bb |= (1ul << sq);
        > > > sq = 57;
        > > > bb |= (1ul << sq);
        > > > sq = 62;
        > > > bb |= (1ul << sq);
        > > >
        > > > Console.WriteLi ne(bb); // this prints 4,755,801,206,5 03,243,842
        > > >
        > > > Now we have this ulong (UInt64) that has the following values in[/color]
        > > binary,hex[color=darkred]
        > > > and decimal:
        > > > 01000010 00000000 00000000 00000000 00000000 00000000 00000000[/color][/color][/color]
        01000010[color=blue][color=green][color=darkred]
        > > > 42 00 00 00 00 00 00 42
        > > > 4,755,801,206,5 03,243,842
        > > >
        > > > I know that it might not be the best way to go about chess programming[/color][/color]
        > but[color=green][color=darkred]
        > > > what I want to do is to convert this ulong bb into either a regular[/color][/color]
        > array[color=green][color=darkred]
        > > > using
        > > >
        > > > (byte[] aByteArray = new byte[64])
        > > > or
        > > > a BitArray using (BitArray aBitArray = new BitArray(64).
        > > >
        > > >
        > > > Will someone please show me the way.
        > > > Thanks
        > > > grs
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • george r smith

          #5
          Re: Type Conversion Problem

          100,
          strange name 100 but to each his own :)

          It worked perfectly, thank you very much.
          george



          Comment

          • Jeffrey Tan[MSFT]

            #6
            RE: Type Conversion Problem


            Hi George,

            Thanks for posting in this group.
            To convert a ulong into a bit array, you can do like this:

            UInt64 bb;
            byte [] storearr=BitCon verter.GetBytes (bb);
            BitArray ba=new BitArray(storea rr);

            Because BitArray implemented IEnumerable interface, then you can use
            IEnumerable.Get Enumerator() to get all the filed of bitarray

            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...