Solved

Converting long to byte array and back

Posted on 2013-11-05
4
703 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

13 Experts available now in Live!

Get 1:1 Help Now