Hex to ascii string conversion

后端 未结 3 903
执念已碎
执念已碎 2020-12-09 11:29

i have an hex string and want it to be converted to ascii string in C. How can i accomplish this??

3条回答
  •  不思量自难忘°
    2020-12-09 11:46

    strtol() is your friend here. The third parameter is the numerical base that you are converting.

    Example:

    #include       /* printf */
    #include      /* strtol */
    
    int main(int argc, char **argv)
    {
        long int num  = 0;
        long int num2 =0;
        char * str. = "f00d";
        char * str2 = "0xf00d";
    
        num = strtol( str, 0, 16);  //converts hexadecimal string to long.
        num2 = strtol( str2, 0, 0); //conversion depends on the string passed in, 0x... Is hex, 0... Is octal and everything else is decimal.
    
        printf( "%ld\n", num);
        printf( "%ld\n", num);
    }
    

提交回复
热议问题