473,320 Members | 1,838 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.

Method to Unpack byte array into a UInt64

Hi Group

I am trying to write a method to unpack a byte array into an Uint64.
But the results are wrong.

public static void UnpackUint64(ref UInt64 dst, byte[] Data,
ref uint Index)
{
dst = Data[Index++];
dst += (UInt64)((Data[Index++] & 0xffffffff) << 08);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 16);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 24);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 32);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 40);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 48);
dst += (UInt64)((Data[Index++] & 0xffffffff) << 56);
}

The method I use to pack the Uint64 into the byte array is

public static void PackUint64(ulong src, byte[] Data, ref uint
Index)
{
Data[Index++] = (byte)(src & 0xff);
Data[Index++] = (byte)((src >08) & 0xff);
Data[Index++] = (byte)((src >16) & 0xff);
Data[Index++] = (byte)((src >24) & 0xff);
Data[Index++] = (byte)((src >32) & 0xff);
Data[Index++] = (byte)((src >40) & 0xff);
Data[Index++] = (byte)((src >48) & 0xff);
Data[Index++] = (byte)((src >56) & 0xff);
}

The Pack looks okay, but I have spent hours on the UnPack and I just
seem to be confusing myself.

Can anyone tell me what is wrong with my unpack method?

Thanks

Stuart

Mar 23 '07 #1
5 8446
On Mar 23, 9:11 am, stuie_nor...@yahoo.com.au wrote:
I am trying to write a method to unpack a byte array into an Uint64.
But the results are wrong.
In what circumstances?
A quick test app works for me, but I've only tried one value.

Can you provide a short but complete program which demonstrates the
problem?
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Personally, I would use BitConverter (or my own EndianBitConverter,
which allows you to specify endian-ness) to do this.

Matters of style you may wish to note:
1) Use "out" instead of "ref" when you don't care about the input
value
2) Prefer return values to pass-by-ref usually - I would certainly
make Unpack return a ulong instead of taking one by reference
3) Parameter names are conventionally camelCased rather than
PascalCased
4) Your method names use "Uint64" instead of "UInt64" (capitalise the
I)

I guess passing the index by reference allows for some useful use
cases, but it's a bit of a pain in others (where you just want to pass
0, for instance).

Jon

Mar 23 '07 #2
Hi Group,

Here is an example that demonstrates the issue with my method. I
would expect the result of the method to display the number after the
PACK and UNPACK.

Pack 2147483648, UnPack 18446744071562067968.

Can anyone tell me what I am doing wrong with my UnPack Method?

Thanks

Stuart

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace UtilityTester
{
class Program
{
public static void PackUint64(ulong src, byte[] Data, ref uint
Index)
{
Data[Index++] = (byte)(src & 0xff);
Data[Index++] = (byte)((src >08) & 0xff);
Data[Index++] = (byte)((src >16) & 0xff);
Data[Index++] = (byte)((src >24) & 0xff);
Data[Index++] = (byte)((src >32) & 0xff);
Data[Index++] = (byte)((src >40) & 0xff);
Data[Index++] = (byte)((src >48) & 0xff);
Data[Index++] = (byte)((src >56) & 0xff);
}
public static void UnpackUint64(ref UInt64 dst, byte[] Data,
ref uint Index)
{
dst = Data[Index++];
dst += (UInt64)((Data[Index++] & 0xffff) << 08);
dst += (UInt64)((Data[Index++] & 0xffff) << 16);
dst += (UInt64)((Data[Index++] & 0xffff) << 24);
dst += (UInt64)((Data[Index++] & 0xffff) << 32);
dst += (UInt64)((Data[Index++] & 0xffff) << 40);
dst += (UInt64)((Data[Index++] & 0xffff) << 48);
dst += (UInt64)((Data[Index++] & 0xffff) << 56);
}
static void Main(string[] args)
{
byte[] b = new byte[10];
uint ipack = 0;
uint iunpack = 0;
UInt64 number64 = 0;
UInt64 result64 = 0;

number64 = 2147483648;
ipack = 0;
PackUint64(number64, b, ref ipack);
iunpack = 0;
result64 = 0;
UnpackUint64(ref result64, b, ref iunpack);
Console.WriteLine("Pack {0}, UnPack {1}",
number64,result64);
Thread.Sleep(50000000);
}
}
}

Mar 23 '07 #3
On Mar 23, 9:35 am, stuie_nor...@yahoo.com.au wrote:
Here is an example that demonstrates the issue with my method. I
would expect the result of the method to display the number after the
PACK and UNPACK.

Pack 2147483648, UnPack 18446744071562067968.

Can anyone tell me what I am doing wrong with my UnPack Method?
Yup - sorry not to have spotted it before. When you're left-shifting,
you're doing it with an int. Instead, you need to cast to a ulong
first. Here's the corrected method:

public static void UnpackUint64(ref UInt64 dst,
byte[] Data,
ref uint Index)
{
dst = Data[Index++];
dst += ((UInt64)Data[Index++]) << 08;
dst += ((UInt64)Data[Index++]) << 16;
dst += ((UInt64)Data[Index++]) << 24;
dst += ((UInt64)Data[Index++]) << 32;
dst += ((UInt64)Data[Index++]) << 40;
dst += ((UInt64)Data[Index++]) << 48;
dst += ((UInt64)Data[Index++]) << 56;
}

Jon

Mar 23 '07 #4
Jon,

Thanks for the reply. That is great. I was just staring at it an not
noticing what is wrong.

Any ideas why I the error

"Cannot implicitily convert type 'int' to 'ushort'. An explicit
conversion exists (are you missing a cast?)"

public static void UnpackUInt16(ref UInt16 dst, byte[] Data,
ref uint Index)
{
dst = (UInt16)Data[Index++];
dst += (UInt16)Data[Index++] << 08; // This line
}

Thanks

Stuart

Mar 24 '07 #5
<st**********@yahoo.com.auwrote:
Thanks for the reply. That is great. I was just staring at it an not
noticing what is wrong.

Any ideas why I the error

"Cannot implicitily convert type 'int' to 'ushort'. An explicit
conversion exists (are you missing a cast?)"
Yes - only the following left shift operators are predefined:

int operator <<(int x, int count);
uint operator <<(uint x, int count);
long operator <<(long x, int count);
ulong operator <<(ulong x, int count);

So in this case, your ushort is converted to an int, and the result is
an int. You need to cast the result to a ushort.

Note that in this case you don't need the cast on the first line, and
the cast on the second line only needs to be for the whole expression -
the only reason you needed the cast for Data[Index++] in the ulong
situation is because you needed to make it use the last of the operator
overloads listed above rather than the first. So your method can be:

public static void UnpackUInt16(ref UInt16 dst, byte[] Data,
ref uint Index)
{
dst = Data[Index++];
dst += (UInt16)(Data[Index++] << 08);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 24 '07 #6

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

Similar topics

5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
6
by: serpent17 | last post by:
Hello, I was looking at this: http://docs.python.org/lib/module-struct.html and tried the following >>> import struct >>> struct.calcsize('h') 2 >>> struct.calcsize('b')
2
by: Nadav | last post by:
How can a UInt64 be converted to a byte ????
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: You don't need the newsgroup to answer this kind of question. Just try it! abcdefghi abcd Notice that the index does not change the original array. Gary Herron
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
5
by: Anders Borum | last post by:
Hi! While implementing a property manager (that supports key / value pairs), I was wondering how to constrain T to a struct or string type. Basically, I guess what I'm looking for is the common...
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
7
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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....

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.