Solved

Converting long to byte array and back

Posted on 2013-11-05
4
718 Views
Last Modified: 2013-11-07
I need to convert a long to a byte array and back again. I can't use BitConverter since the Micro Framework version of C# doesn't support it. Here's the code I'm using:
            long origLong = long.MinValue;
            byte[] bytes = new byte[8];
            for (int i = 0; i < 8; i++) {
                bytes[i] = (byte)(origLong >> (i * 8) & 0xFF);
            }
            long newLong = 0;
            for (int i = 0; i < 8; i++) {
                newLong = newLong | (long)(bytes[i] << (i * 8));
            }
            for (int i = 0; i < bytes.Length; i++) {
                Debug.Write(bytes[i] + " ");
            }
            Debug.Print("");
            Debug.Print("orig: " + origLong + ", new: " + newLong);

Open in new window

The results in the Output are:
0 0 0 0 0 0 0 128 
orig: -9223372036854775808, new: -2147483648

Open in new window

It looks like the conversion to a byte array is good but the conversion back to long is not. Also, on the statement with the OR operator (#8), I get the warning "Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first".

How can I do this?
0
Comment
Question by:BlearyEye
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 39626239
The problem exists because of a wrong casting order in the line 8. Cast a byte to long before shifting, change to:

newLong = newLong | ((long)bytes[i] << (i * 8));

Open in new window

0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 39627401
Perfect, thanks.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 39630554
As a follow-up, I've come across two implementations of BitConverter for MF:

https://bitbucket.org/aalmada/hydramf/src/779d1c524a9c/HydraMF.Hardware/BitConverter.cs
http://forums.netduino.com/index.php?/topic/308-bitconverter/, and accompanying comments (there are bugs in the posted code).
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39631282
Thanks, interesting!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now