>Can you give an example of the input data, and show what you want to end up with.
I think this may be an example.
Why not try the standard library routine strtol?
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
/*
#define NDEBUG
*/
short foo(const char *buffer, int offset)
{
char text[5] = {0};
memcpy(text, &buffer[offset], sizeof(text) - 1);
#ifndef NDEBUG
printf("text = \"%s\", ", text);
#endif
return (short)strtol(text, NULL, 16);
}
int main(void)
{
const char buffer[14] =
{
2, 6, 060, 060, 060, 060, 060, 060, 060, 0106, 0106, 0105, 061, 3
};
int i;
for ( i = 2; i < 10; i++ )
{
short result = foo(buffer, i);
printf("result = %hd\n", result);
}
return 0;
}
My output:
Code:
text = "0000", result = 0
text = "0000", result = 0
text = "0000", result = 0
text = "0000", result = 0
text = "000F", result = 15
text = "00FF", result = 255
text = "0FFE", result = 4094
text = "FFE1", result = -31