Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:02:41+00:00 2026-06-15T18:02:41+00:00

If I want to convert 4 bytes into an int, I can do this:

  • 0

If I want to convert 4 bytes into an int, I can do this:

        byte[] b = BitConverter.GetBytes(i1);
        int i2 = BitConverter.ToInt32(b,0);
        int i3 = b[0] | (b[1]<<8) | (b[2]<<16) | (b[3]<<24);

and then i1,i2,i3 will all equal.

but how do I do the same for a uint? This:

        uint u1 = uint.MaxValue-1000;
        byte[] b = BitConverter.GetBytes(u1);
        uint u2 = BitConverter.ToUInt32(b,0);
        uint u3 = (uint)(b[0] | (b[1]<<8) | (b[2]<<16) | (b[3]<<24));

results in a overlflow for large uints.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:02:42+00:00Added an answer on June 15, 2026 at 6:02 pm

    It would only throw that exception if in a checked context. See: http://msdn.microsoft.com/en-us/library/y3d0kef1(v=vs.80).aspx.

    No exception:

    uint u1 = uint.MaxValue - 1000;
    byte[] b = BitConverter.GetBytes(u1);
    uint u2 = BitConverter.ToUInt32(b, 0);
    uint u3 = (uint) (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));
    

    exception:

    checked
    {
        uint u1 = uint.MaxValue - 1000;
        byte[] b = BitConverter.GetBytes(u1);
        uint u2 = BitConverter.ToUInt32(b, 0);
        uint u3 = (uint) (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));
    }
    

    no exception

    checked
    {
        unchecked
        {
            uint u1 = uint.MaxValue - 1000;
    
            byte[] b = BitConverter.GetBytes(u1);
            uint u2 = BitConverter.ToUInt32(b, 0);
            uint u3 = (uint) (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24));
    
            Console.WriteLine(u1 + " " + u2 + " " + u3);
        }
    }
    

    Make sure you’re not compiling with the /checked option.

    The exception is thrown by casting from int to uint. Using the shift operator on the bytes (the line with uint u3 = …) implicitly cast them to int. A uint with the MSB on (“1”) is a negative int which is out of range for uint. Using int causes no such exception because there is no explicit cast which might elicit an overflow exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a string of bytes into an int in python? Say
I just wrote this code to convert a GUID into a byte array. Can
I want convert an int into 2 bytes representing that int. Probably must use
I want to convert a C-style string into a byte-vector. A working solution would
I want to convert an int to a byte[2] array using BCD. The int
I have a char byte that I want to convert to an int. Basically
I want to convert a string into a byte array. (Yes) I have seen
I want to convert a single number of bytes, into a file size (that
I'm reading from a binary file and want to convert the bytes to US
I want to convert all the URLs in a javascript string to links, in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.