1

I am trying to convert a hex string to its equivalent ASCII. I am given a "hex" value of a string as a string, i.e instead of "ABCD" I get "41424344". I just need to extract 41 which will be my hexvalue and recode as "ABCD". Here is what I have.

   int main(int argc, char *argv[]){

    char *str = "ABCD";
    unsigned int val = 0;
    int i = 0;
    int MAX = 4;
    for (i = 0; i<MAX; i++){
        val = (str[i] & 0xFF);
        //printf("dec val= %d\n", val);
        //printf("hex val= %02x\n", val);
    }
    val = 0;
    char *hexstr = "41424344";
    char *substr = (char*)malloc(3);
    char *ptr = hexstr;

    for (i = 0; i<8; i++){
        strncpy(substr, ptr, 2);
        printf("substr = %s\n", substr);
        int s = atoi(substr);
        printf("s= %d\n", s);
        ptr= ptr+2;
        i = i+2;
    }
    return 0;

}

The thing is from here on, I have to make this "s" value to be a hex value and not an int. How can this be done?

UPDATE:

Here is what I have after your answers:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){

    char *str = "ABCD";
    unsigned int val;
    val = 0;
    int i = 0;
    int MAX = 4;
    for (i = 0; i<MAX; i++){
        val = (str[i] & 0xFF);
        //printf("dec val= %d\n", val);
        //printf("hex val= %02x\n", val);
    }
    val = 0;
    char *hexstr = "41424344";
    char *substr = (char*)malloc(3);
    char *ptr = hexstr;
    char *retstr = (char *)malloc(5);
    char *retptr = retstr;

    for (i = 0; i<8; i+1){
        strncpy(substr, ptr, 2);
        printf("substr = %s\n", substr);
        int s = strtol(substr, NULL, 16);
        printf("s= %d\n", s);
        ptr= ptr+2;
        i = i+2;
        sprintf(retptr, "%c", s);
        retptr = retptr +1;
    }
    printf("retstr= %s\n", retstr);
    return 0;

}
4
  • you have strtol to convert a string in an other base and %x or %X in printf to display an hexa value Commented Jul 16, 2013 at 9:25
  • 4
    Allocate 3 bytes for substr. Forgot '\0' ?? Commented Jul 16, 2013 at 9:26
  • related: Handling of conversions from and to hex Commented Jul 16, 2013 at 9:27
  • Thanks Jeyaram! silly me :) Commented Jul 16, 2013 at 9:37

1 Answer 1

2

Change your "s" variable line from

int s = atoi(substr);

to

int s = strtol(substr, NULL, 16);

Sign up to request clarification or add additional context in comments.

4 Comments

substr doesn't have a nul terminator so the return from atoi will still be undefined
yeah, also there's an interesting typo in the for loop increment step. i+i. anyway updated the answer hope it helps
hi sudopunk, why do I get a warning in the for loop statement: statement has no effect?
your loop needs to assign a value to i. (i = 0; i<8; i+1) should be changed to (i = 0; i<8; i=i+2) and you should remove the following statement inside the for loop i = i+2;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.