Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *text2hex(const char *const string) {
- const int n = strlen(string);
- char *hex = malloc(sizeof(char) * (n * 2 + 1));
- if (hex == NULL) {
- fprintf(stderr, "failed to alloc!\n");
- exit(EXIT_FAILURE);
- }
- hex[0] = 0;
- for (int i = 0; i < n; i++) {
- char aux[2] = {};
- sprintf(aux, "%x", (int)string[i]);
- strcat(hex, aux);
- }
- return hex;
- }
- int main(void) {
- char s[] = "abc", *res = NULL;
- res = text2hex(s);
- puts(res);
- free(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement