473,320 Members | 1,535 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,320 software developers and data experts.

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.WriteLine(bb); // this prints 4,755,801,206,503,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,503,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
Nov 15 '05 #1
5 2280
100
Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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.WriteLine(bb); // this prints 4,755,801,206,503,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,503,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

Nov 15 '05 #2
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" <10*@100.com> wrote in message
news:eK*************@tk2msftngp13.phx.gbl...
Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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.WriteLine(bb); // this prints 4,755,801,206,503,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,503,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


Nov 15 '05 #3
100
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= 0x4200000000000042ul;

//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" <gs****@budgetext.com> wrote in message
news:ub**************@TK2MSFTNGP12.phx.gbl...
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" <10*@100.com> wrote in message
news:eK*************@tk2msftngp13.phx.gbl...
Hi george,
Try to use BitConverter.GetBytes static method. It has overload for UInt64
and it is what you need I believe.

HTH
B\rgds
100
"george r smith" <gs****@budgetext.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
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.WriteLine(bb); // this prints 4,755,801,206,503,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,503,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



Nov 15 '05 #4
100,
strange name 100 but to each his own :)

It worked perfectly, thank you very much.
george

Nov 15 '05 #5

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

Because BitArray implemented IEnumerable interface, then you can use
IEnumerable.GetEnumerator() 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.

Nov 15 '05 #6

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

Similar topics

12
by: Aaron Watters | last post by:
I'm doing a heart/lung bypass procedure on a largish Python program at the moment and it prompted the thought that the methodology I'm using would be absolutely impossible with a more "type safe"...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
1
by: Jimmy Seow | last post by:
Dear All, Hope somebody can help me with this. I have created a class and have got it to serialize and deserialize using XmlSerialization without any problems. I've also read the documentation...
11
by: RWC | last post by:
Hello, I'm having trouble converting code in Access XP / 2002. I have some code that declares an variable "as database" in Access 97, which is not recognized in Access XP. I've tried to find a...
4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.