473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.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
Nov 15 '05 #1
5 5344
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]
- mv*@spam.guard.caspershouse.com

"george r smith" <gs****@budgetext.com> wrote in message
news:O1*************@tk2msftngp13.phx.gbl...
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

Nov 15 '05 #2
george r smith <gs****@budgetext.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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
Nov 15 '05 #4
george r smith <gs****@budgetext.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Ennis | last post by:
If I run the following code: Byte bytes = System.Text.Encoding.ASCII.GetBytes("test"); return BitConverter.ToString(bytes); I get the string "74-65-73-74" back. My question is, what is the...
1
by: Gabe Jahn | last post by:
Basically I've used the BitConverter to convert a byte into a string. Now how do I convert it back to a byte? Hopefully without creating my own algorithm to accomplish this? Thanks...
2
by: Opa1 | last post by:
using BitConverter.ToString() in Compact Framework has some bugs. The resulting hex string does not have a consistent format for each byte converted from the input byte array. for example:...
3
by: Timothy V | last post by:
Hi, I have a byte that i want to convert into a string. I use the BitConverter.ToString() method. Now, how do I convert that string back into a byte? Thanks in advance, Tim.
1
by: Michael Davidov | last post by:
byte array = {0x00,0x04}; short ashort = BitConverter.ToInt16(array,0); Console.WriteLine(ashort.ToString()); Why am I wrong to think that this should return a 4 instead of 1024? Also, is...
7
by: Lenn | last post by:
This probably something stupid, or I am missing some fundemantal concept, but I can't figure this one out. Consider the following code: byte bd = new byte; bd = 0x00; bd = 0x01; ...
5
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. I have a problem with getting short value from 2 byte array. I have this code. There are 2 short values in bytes. byte cast = { 18, 152, 00, 80 }; Int32 port = BitConverter.ToInt16(cast,...
1
by: DR | last post by:
mySqlBytes.buffer is getting converted to BigEndian even though both SQL server 2005 and the CLR function are on the same machine which shows BitConverter.IsLittleEndian == true in tsql: select...
5
by: DaveD | last post by:
Can anyone help me get this compiled ? void Write<T>(T val) { byte bytes = BitConverter.GetBytes(val); Array.Reverse(bytes); writer.Write(bytes); } The problem is that for T=bool,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.