How to convert an ASCII character into a hex value in C

2 Answers

0 votes
#include <stdio.h>

int main() {
    char ascii_char = 'A'; 
    char hex_value[3]; 

    // Convert ASCII character to hex value
    sprintf(hex_value, "%02X", ascii_char);

    printf("The hexadecimal value of '%c' is: 0x%s\n", ascii_char, hex_value);

    return 0;
}



/*
run:

The hexadecimal value of 'A' is: 0x41

*/

 



answered Nov 30, 2024 by avibootz
0 votes
#include <stdio.h>

int main() {
    char ascii_char = 'A'; 

    printf("The hexadecimal value of '%c' is: 0x%X\n", ascii_char, (int)ascii_char);

    return 0;
}



/*
run:

The hexadecimal value of 'A' is: 0x41

*/

 



answered Nov 30, 2024 by avibootz

Related questions

1 answer 104 views
1 answer 143 views
1 answer 133 views
1 answer 147 views
1 answer 142 views
2 answers 164 views
...