Results 1 to 7 of 7

Thread: [2.0] have a number loop instead of overflow?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2.0] have a number loop instead of overflow?

    Well Sometimes I have a number that causes the integer to overflow when adding. Now is there a way that instead of overflowing it resets to 0 and continues adding? I was thinking making it a int64 and if it is bigger then an int subtracting an int but is there another easier way?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] have a number loop instead of overflow?

    If an Int32 value overflows then an exception is thrown. Catch the exception and reset your value.

    OR

    Use an Int64 and test its value against Int32.MaxValue, resetting if it becomes greater.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] have a number loop instead of overflow?

    Now why would this error? exact same thing in C++ works fine.
    ret[1] = CurByte ^ CurEncByte;
    Curbyte and CurEncByte are both a byte and ret is a byte array but it gives me the error.

    Error 1 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\High6\My Documents\Visual Studio 2005\Projects\CoProx\CoProx\CoClient.cs 38 22 CoProx

    any idea?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] have a number loop instead of overflow?

    The result of this:
    C# Code:
    1. CurByte ^ CurEncByte
    is an int, not a byte, which is why the error message is telling you that you cannot implicitly convert it to a byte, which you're trying to do by assigning it to a byte variable.

    Remember: C++ and C# are NOT the same language. The fact that something works in one has no bearing on whether it will work in the other. If they were the same language then they'd have the same name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] have a number loop instead of overflow?

    Quote Originally Posted by jmcilhinney
    The result of this:
    C# Code:
    1. CurByte ^ CurEncByte
    is an int, not a byte, which is why the error message is telling you that you cannot implicitly convert it to a byte, which you're trying to do by assigning it to a byte variable.

    Remember: C++ and C# are NOT the same language. The fact that something works in one has no bearing on whether it will work in the other. If they were the same language then they'd have the same name.
    Ty, fixed it by converting them to int then back to byte.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] have a number loop instead of overflow?

    So what's the actual purpose here? To get the lowest order byte from that result and just ignore the rest?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] have a number loop instead of overflow?

    Quote Originally Posted by jmcilhinney
    So what's the actual purpose here? To get the lowest order byte from that result and just ignore the rest?
    I was converting an encryption algorithm from asm to C#

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width