Maybe this can help.
The program expects a number of decimal inputs (max 50). It prints the corresponding hex value and append the char to a string (zero terminated char array). Finally, it prints the string.
#include <stdio.h>
int main(void) {
const int N = 50;
int i = 0;
char text[N+1]; // Make an array to hold the string
text[0] = '\0'; // Zero terminate it
int ch;
while(i < N)
{
if (scanf("%d", &ch) != 1) // Read decimal from stdin
{
break; // Break out if no decimal was returned
}
printf("%02X ", (ch & 0x00FF));
text[i] = (ch & 0x00FF); // Add the new char
text[i+1] = '\0'; // Add a new string termination
++i;
}
printf("\n");
printf("%s\n", text);
return 0;
}
Input:
65 66 67 68
Output:
41 42 43 44
ABCD
Or this alternative which read a string char-by-char until it sees a newline:
#include <stdio.h>
int main(void) {
const int N = 50;
int i = 0;
char text[N+1];
text[0] = '\0';
char ch;
while(i <= N)
{
if (scanf("%c", &ch) != 1 || ch == '\n')
{
break;
}
printf("%02X ", ch);
text[i] = ch;
text[i+1] = '\0';
++i;
}
printf("\n");
printf("%s\n", text);
return 0;
}
Input:
coding is fun
Output:
63 6F 64 69 6E 67 20 69 73 20 66 75 6E
coding is fun