1. ## hex increment

hello, i would like some best way to increment in hex.
example:

Code:
`uint8_t key={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0xFF};`
increment value and result:

Code:
`uint8_t key={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x31,0x00};`
any sugestion?
many thanks  Reply With Quote

2. Code:
```for ( i = 15 ; i >= 0 ; i-- ) {
if ( ++key[i] ) break;
// continues if a value rolls over from 0xff to 0x00
}```  Reply With Quote

3. Originally Posted by Salem Code:
```for ( i = 15 ; i >= 0 ; i-- ) {
if ( ++key[i] ) break;
// continues if a value rolls over from 0xff to 0x00
}```
Maybe, like this?
Code:
```#include <stdio.h>

void incr( unsigned char * );
void show( char *, unsigned char * );

int main( void )
{
static unsigned char keys = {  = 0x30, 0x30, 0xff };

show( "before", keys );
incr( keys );
show( "after ", keys );
}

/* OBS: Salem's code is better than this one! */
void incr( unsigned char *p )
{
unsigned int i, t;

/* using 'unsigned int' because has more bits than 'unsigned char'.
Anything bigger then 255 will be used to increment the previous item
in the array (carry). */
t = p;
p = ++t;

/* Here, add carry-outs, if any. */
for ( i = 14; i >= 0; i-- )
{
if ( t < 256 )
break;
t = p[i];
p[i] = ++t;
}
}

void show( char *s, unsigned char *p )
{
int i;

printf( "%s: {", s );
for ( i = 0; i < 15; i++ )
printf( "0x%02x, ", p[i] );
printf( "0x%02x }\n", p[i] );
}```
If you want to use SSSE3 (on x86):
Code:
```void incr2( unsigned char *p )
{
__m128i *a = (__m128i *)p;
__m128i b = _mm_set_epi8( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );

// the array is backwards (big endian)...
*a = _mm_shuffle_epi8( *a, b );

(*a)++;

*a = _mm_shuffle_epi8( *a, b );
}```  Reply With Quote

4. many thanks , it works. Originally Posted by Salem Code:
```for ( i = 15 ; i >= 0 ; i-- ) {
if ( ++key[i] ) break;
// continues if a value rolls over from 0xff to 0x00
}```  Reply With Quote

Popular pages Recent additions hex, increment, result, sugestion, uint8_t 