Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting long to byte array and back

Posted on 2013-11-05
4
Medium Priority
?
887 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 30

Accepted Solution

by:
anarki_jimbel earned 2000 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 30

Expert Comment

by:anarki_jimbel
ID: 39631282
Thanks, interesting!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

971 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