Results 1 to 3 of 3

Thread: [RESOLVED] Increment byte[] by 0x01

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    47

    Resolved [RESOLVED] Increment byte[] by 0x01

    I was wondering if anyone had simple/fast code for incrementing a byte[] by a single value. So for example:

    Code:
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    becomes...

    Code:
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
    and...

    Code:
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}
    turns into...

    Code:
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}
    .

    Thanks,
    WB3000

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Increment byte[] by 0x01

    Would this recursive function be good enough?
    Code:
            private void incrementAtIndex(byte[] array, int index) {
                if (array[index] == byte.MaxValue) {
                    array[index] = 0;
                    if(index > 0)
                        incrementAtIndex(array, index - 1);
                }
                else {
                    array[index]++;
                }
            }
    Call it by passing the array and the upper bound to it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2007
    Posts
    47

    Re: Increment byte[] by 0x01

    Seems to be working fine. Thanks a ton!

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