2

I apologize in advance if my question is not clear enough, I don't have a lot of experience with c# and I encounter an odd problem. I am trying to convert an int into an array of two bytes (for example: take 2210 and get: 0x08, 0xA2), but all I'm getting is: 0x00, 0xA2, and I can't figure out why. Would highly appreciate any advice. (I've tried reading other questions regarding this matter, but couldn't find an helpful answer)

my code:

        profile_number = GetProfileName(); // it gets the int 
        profile_num[0] = (byte) ((profile_number & 0xFF00));
        profile_num[1] = (byte) ((profile_number & 0x00FF));
        profile_checksum = CalcProfileChecksum();

//Note: I'm referring to a 2-byte array, so the answer to the question regarding 4-byte arrays does not help me.

4
  • 6
    Size of int is 4 bytes. Commented Jul 30, 2015 at 6:40
  • possible duplicate of C# int to byte[] Commented Jul 30, 2015 at 6:44
  • You expect to always get an array of two bytes? Commented Jul 30, 2015 at 6:45
  • Yes, I'm building a message that is being sent to an hardware that expects 2 byte array. Commented Jul 30, 2015 at 6:54

2 Answers 2

7

You need to shift the 1st byte:

 //profile_num[0] = (byte) ((profile_number & 0xFF00));
 profile_num[0] = (byte) ((profile_number & 0xFF00) >> 8);
 profile_num[1] = (byte) ((profile_number & 0x00FF));
Sign up to request clarification or add additional context in comments.

4 Comments

Always better to say why the example code isn't working rather than just "here's a different way to do it" :)
Originally I've tried ">> 16" with no success, but your solution works, Thank you!
This is for Int16 I supose not Int32 that has 4 bytes
@GeorgeLica - it works for any int but only uses the lower 2 bytes.
0

First I thought that this is the simplest way:

public static byte[] IntToByteArray(int value)
{
    return (new BigInteger(value)).ToByteArray();
}

But i realised that ToByteArray returns only needed bytes. If the value is small (under 256) then a single byte will be returned. Another thing that I noticed is that values are reversed in the returned array so that the byte that is in the right (the most insignificant byte) is found in the left. So I come with a little revision:

public static byte[] IntToByteArrayUsingBigInteger(int value, int numberOfBytes)
    {
        var res = (new BigInteger(value)).ToByteArray().Reverse().ToArray();
        if (res.Length == numberOfBytes)
            return res;

        byte[] result = new byte[numberOfBytes];

        if (res.Length > numberOfBytes)
            Array.Copy(res, res.Length - numberOfBytes, result, 0, numberOfBytes);
        else
            Array.Copy(res, 0, result, numberOfBytes - res.Length, res.Length);

        return result;
    }

I know that this does not compare with the performance of having bitwise operations but for the sake of learning new things and if you prefeer to use high level classes that .NET provides instead of going low level and use bitwise operators i think it's a nice alternative.

3 Comments

See the link for the proposed duplicate, System.Convert is the shorter path.
First, thank you very much for this answer, but wouldn't it give me a 4 bytes array?
I changed my code so that you can call my BigInteger version so that it gets for you whatever number of bytes you need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.