#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA_SIZE 1024
static int convert_string2hex(unsigned char *in_data, int in_data_len, unsigned char *out_data, int *out_data_len)
{
int i;
int loop_count;
int convert_point = 0;
int mem_point = 0;
unsigned char convert_result;
unsigned char temp[3] = {0};
if (in_data == NULL || in_data_len <= 0 || out_data == NULL || out_data_len == NULL || (in_data_len % 2) != 0) {
printf("invalid parameters\n");
return -1;
}
for (i = 0; i < in_data_len; i++) {
if ((in_data[i] < '0') || (in_data[i] > 'f') || ((in_data[i] > '9') &&(in_data[i] < 'A'))) {
printf("out of range\n");
return -1;
}
}
loop_count = in_data_len / 2;
memset(out_data, 0x00, *out_data_len);
*out_data_len = 0;
for (i = 0; i < loop_count; i++) {
memset(temp, 0x00, sizeof(temp));
memcpy(temp, in_data + convert_point, 2);
convert_point += 2;
convert_result = strtoul(temp, NULL, 16);
memcpy(out_data + mem_point, &convert_result, sizeof(unsigned char));
mem_point += sizeof(unsigned char);
*out_data_len += sizeof(unsigned char);
}
return 0;
}
int main(int argc, char const *argv[])
{
unsigned char in_data[MAX_DATA_SIZE] = {0};
int in_data_len = MAX_DATA_SIZE;
unsigned char out_data[MAX_DATA_SIZE] = {0};
int out_data_len = MAX_DATA_SIZE;
int i;
scanf("%s", in_data);
in_data_len = strlen(in_data);
convert_string2hex(in_data, in_data_len, out_data, &out_data_len);
for (i = 0; i < out_data_len; i++) {
printf("0x%02x ", out_data[i]);
}
printf("\n");
return 0;
}