I have a pseudoheader in a string, the first 12 bits are the file size, the next 4 are the buffer size, and the last 12 are an offset. I want to store each one of this in a variable, I could get the string values, but atoi
is not working properly when converting those to integers
The program looks like this:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void readheader(char header[29], int filesize, int buffersize, int offset) {
char fsize[13], bsize[5], oset[13];
memcpy(fsize, &header[0], 12);
fsize[12] = '\0';
memcpy(bsize, &header[12], 4);
bsize[4] = '\0';
memcpy(oset, &header[16], 12);
oset[12] = '\0';
printf("String Values: filesize: %s, buffersize: %s, offset: %s\n", fsize, bsize, os$
filesize = atoi(fsize);
buffersize = atoi(bsize);
offset = atoi(oset);
}
void main() {
char test[] = "0000000051422048000000004096";
int a, b, c;
readheader(test, a, b, c);
printf("Integer values: filesize: %d, buffersize: %d, offset: %d\n", a, b, c);
}
And the output looks like this:
String Values: filesize: 000000005142, buffersize: 2048, offset: 000000004096
Integer values: filesize: 0, buffersize: -1036019427, offset: 22039