[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
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.
Re: Increment byte[] by 0x01
Seems to be working fine. Thanks a ton! :D