BitConverter.ToUInt64 Question

G

george r smith

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.ToUInt64(aByteArray,0);
Console.WriteLine(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
 
N

Nicholas Paldino [.NET/C# MVP]

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

Jon Skeet [C# MVP]

george r smith said:
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.ToUInt64(aByteArray,0);
Console.WriteLine(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.

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

Because longs and ulongs are only 8 bytes long.
I am trying hard to understand
BitArray class, the BitConverter class and the interplay between them.

I'm not sure where BitArray comes in here.

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

george r smith

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
 
J

Jon Skeet [C# MVP]

george r smith said:
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.

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

Jeffrey Tan[MSFT]

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=BitConverter.GetBytes(bb);
BitArray ba=new BitArray(storearr);

Then you can use System.Collections.IEnumerator to enumerate every bits of
the bitarray.
For more information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcollectionsbitarrayclasstopic.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top