After doing a little thinking about the "accessing memory" discussion, etc. I thought I had the solution (but you might be able to understand this without reading that).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int translate_lilend (int LE, int size) {
short int len=size*2, i, letr[size];
char hex[len+1], chr[3];
printf("%d\t%d\n",LE,size);
sprintf(hex, "%x\n",LE);
for (i=0;i<len;i+=2){
chr[0]=hex[len-(2+i)];
chr[1]=hex[len-(1+i)];
chr[3]=0;
puts(chr);
letr[i/2]=0;
letr[i/2]=strtol(chr,NULL,16);
printf("%d\t%d\n", i/2,letr[i/2]);
}
for (i=0;i<size;i++) printf("letr:%d\n",letr[i]);
for (i=0;i<size;i++) hex[i]=letr[i];
hex[i+1]=0;
puts(hex);
}
int main(void) {
translate_lilend(1952540788,4); // a word is 4-bytes
}
Now, I would have thought the output from this would be consistent, even if I had some bit wrong. Instead,
Code:
1952540788 4
74
0 116
68
1 104
61
2 97
74
3 116 up to here, everything good, stays the same-- then
letr:-4108 suddenly, the array has changed! And it's different every time the
letr:18342 program is run!
letr:-17248
letr:18321
����6
Nb. I've done very little work with numerical arrays thus far in my learning. What's wrong?