Thread: hex increment

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    hex increment

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

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


    Code:
    uint8_t key[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x31,0x00};
    any sugestion?
    many thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    for ( i = 15 ; i >= 0 ; i-- ) {
      if ( ++key[i] ) break;
      // continues if a value rolls over from 0xff to 0x00
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    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[16] = { [13] = 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[15];
      p[15] = ++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 );
    }
    Last edited by flp1969; 10-19-2019 at 07:48 AM.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    many thanks , it works.

    Quote Originally Posted by Salem View Post
    Code:
    for ( i = 15 ; i >= 0 ; i-- ) {
      if ( ++key[i] ) break;
      // continues if a value rolls over from 0xff to 0x00
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counter will not increment
    By serge in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2019, 03:20 AM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  3. increment char
    By Okiura in forum C Programming
    Replies: 12
    Last Post: 05-28-2011, 09:12 PM
  4. can't get loop to increment
    By rivkyfried1 in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 04:03 AM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread