Create array of 16 byte values with leading 0's and final byte being each hex value

Viewed 35

I am looking to make an array that contains 256 elements each element should be incremented hence. array[0] should be 00000000000000000000000000000000 and array[1] should be 00000000000000000000000000000001 all the way up to array[255] should be 000000000000000000000000000000FF Currently all I have is

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    unsigned char arr[16] = {[0 ... 15] = 0x00};
    memset(arr, 0x00, sizeof(arr));
    printf("%s\n",arr);
}

Which currently just prints a new line and that's all. My goal is to have something similar to:

...
int main()
{
    unsigned char arr[256][16];
    for(int i=0;i<256;i++)
    {
       memset(arr[i],0x00,sizeof(arr[i]));
       //Add code to create each 16 byte hex string
       printf("%s\n",arr[i]); //Print each 16 byte string to make sure they're correct
    }
}
1 Answers

From the above comments, this worked:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    unsigned char arr[256][33];
    for(int i=0;i<256;i++)
    {
       snprintf(arr[i], sizeof arr[i], "%032x", i);
       printf("%s\n",arr[i]); //Print each 16 byte string to make sure they're correct
    }
}
Related