473,657 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BitConverter.To UInt64 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
Nov 15 '05 #1
5 5367
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.co m

"george r smith" <gs****@budgete xt.com> wrote in message
news:O1******** *****@tk2msftng p13.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.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

Nov 15 '05 #2
george r smith <gs****@budgete xt.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.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.
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.co m>
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****@budgete xt.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.co m>
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=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:
http://msdn.microsoft.com/library/de...us/cpref/html/
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.

Nov 15 '05 #6

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

Similar topics

4
16237
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 easiest way to convert the string back to "test"? Can it be done without breaking the string and using a loop, etc.? I have tried many methods, but can't find a way without using a loop. It
1
3408
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
2128
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: Byte array with values, (9,0,0,0,115,105,103,110,97,116,117,114,101,224,0,0,0,51,0,0,0,1) result in,
3
6397
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
2216
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 there a way to get a short value of 4 from those 2 bytes without switching their possition in the byte array?
7
8118
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; System.Int16 a;
5
4576
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, 0); // -26606 - wrong! Int32 port2 = BitConverter.ToInt16(cast, 2); // 20480 - wrong! //port and port2 get not correct values //and I found solution in google short result1 = (short)(((short)cast) | (short)(cast * 256)); //4760 -
1
1990
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 * from dbo.MY_CLR_FUNCTION(cast(1024 as binary(4))) public static int MY_CLR_FUNCTION(SqlBytes mySqlBytes) in debugger binaryData.buffer now shows bigendian!! 0 byte
5
5957
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, BitConverter.GetBytes(bool) won't compile. In C++ I would have created an explicit instantiation like
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.