none
Byte array sum

Answers

  • Hi lctk,

    You could look into the below thread answer. Hope this helps you.

    SUM of Byte Array values in C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by lctk Wednesday, April 12, 2017 12:19 AM
    Thursday, April 6, 2017 1:19 PM
    Moderator
  • Hi lctk,

    Thank you for posting here.

    And what type of sum you want to get? int or byte[]?

    For your question, you could try the following code for reference.

    static void Main(string[] args)
            {
                Byte[] b1 = { 0xEC, 0x00, 0x00, 0x00 };//sum=236
                int sum1 = BitConverter.ToInt32(b1, 0);// first way
    
                byte[] b = BitConverter.GetBytes(sum1);//convert sum1 to byte[]
    
                int sum2 = 0;
                foreach (var item in b1)
                {
                    int s = (int)item;
                    sum2+=s;
                };
                Console.WriteLine(sum2);//second 
    
                int sum3 = 0;
                for (int i = 0; i < b1.Length; i++)
                {
                    sum3 += (int)b1[i];
                }
                Console.WriteLine(sum3);//third
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 7, 2017 3:50 AM
    Moderator
  • If you just want the sum of all values in byte array, you can just use:

                byte[] arrByte = new byte[] { 1, 2, 3, 4 };
                int sum = arrByte.Select(x => (int)x).Sum();    // expand field size to avoid overflow
                Console.WriteLine(sum);
    See other's answer for way to sum two byte array that is actually a 64-bit integer value.

    • Edited by cheong00Editor Friday, April 7, 2017 4:50 AM
    • Proposed as answer by RohitArora Friday, April 7, 2017 5:41 AM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:18 AM
    Friday, April 7, 2017 4:49 AM
    Answerer
  • If you are interested, try this too:

       byte[] arrByte = new byte[] { 100, 200, 30, 40 };

       int sum = arrByte.Aggregate( 0, ( a, b ) => a + b );

    • Marked as answer by lctk Wednesday, April 12, 2017 12:18 AM
    Tuesday, April 11, 2017 6:08 AM

All replies

  • Hi lctk,

    You could look into the below thread answer. Hope this helps you.

    SUM of Byte Array values in C#


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by lctk Wednesday, April 12, 2017 12:19 AM
    Thursday, April 6, 2017 1:19 PM
    Moderator
  • Is there a simple code that, see don't understand

    please verify my account

    Thursday, April 6, 2017 11:42 PM
  • Hi lctk,

    Thank you for posting here.

    And what type of sum you want to get? int or byte[]?

    For your question, you could try the following code for reference.

    static void Main(string[] args)
            {
                Byte[] b1 = { 0xEC, 0x00, 0x00, 0x00 };//sum=236
                int sum1 = BitConverter.ToInt32(b1, 0);// first way
    
                byte[] b = BitConverter.GetBytes(sum1);//convert sum1 to byte[]
    
                int sum2 = 0;
                foreach (var item in b1)
                {
                    int s = (int)item;
                    sum2+=s;
                };
                Console.WriteLine(sum2);//second 
    
                int sum3 = 0;
                for (int i = 0; i < b1.Length; i++)
                {
                    sum3 += (int)b1[i];
                }
                Console.WriteLine(sum3);//third
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 7, 2017 3:50 AM
    Moderator
  • If you just want the sum of all values in byte array, you can just use:

                byte[] arrByte = new byte[] { 1, 2, 3, 4 };
                int sum = arrByte.Select(x => (int)x).Sum();    // expand field size to avoid overflow
                Console.WriteLine(sum);
    See other's answer for way to sum two byte array that is actually a 64-bit integer value.

    • Edited by cheong00Editor Friday, April 7, 2017 4:50 AM
    • Proposed as answer by RohitArora Friday, April 7, 2017 5:41 AM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:18 AM
    Friday, April 7, 2017 4:49 AM
    Answerer
  • Hi lctk,

    If your question has been solved, please mark the useful reply as answer.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 11, 2017 2:42 AM
    Moderator
  • If you are interested, try this too:

       byte[] arrByte = new byte[] { 100, 200, 30, 40 };

       int sum = arrByte.Aggregate( 0, ( a, b ) => a + b );

    • Marked as answer by lctk Wednesday, April 12, 2017 12:18 AM
    Tuesday, April 11, 2017 6:08 AM