Solved

Converting long to byte array and back

Posted on 2013-11-05
4
809 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

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 30

Expert Comment

by:anarki_jimbel
ID: 39631282
Thanks, interesting!
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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