Last active
September 26, 2017 21:39
-
-
Save StefanoBelli/9cce425f5e8a940e6f07e713c105eba3 to your computer and use it in GitHub Desktop.
String to byte array (hex base). String must not contain any space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ctype.h> | |
static inline | |
int hex_alpha_only(const char* string) | |
{ | |
for(;*string;*string++) { | |
if(!( (*string >= 'a' && *string <= 'f') || | |
(*string >= 'A' && *string <= 'F') || | |
(*string >= '0' && *string <= '9') | |
)) //if...end | |
return 0; | |
} | |
return 1; | |
} | |
int strtoba_h(const char* from, unsigned char* target_array, const size_t target_array_size) | |
{ | |
if(!hex_alpha_only(from)) | |
return -1; | |
for(size_t i=0;i<target_array_size;i++) { | |
sscanf(from,"%2hhx", &target_array[i]); | |
from += 2; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment