0

I'm using a loop in C and trying to determine how fprintf works.

fprintf(out, "%02X", (int)(ch & 0x00FF); 

This statement prints out a single hex value char for every iteration of the loop.

Can I store this in a variable or char array?

How can I concatenate this into one large string, and then write it to the file?

Do I have to check the total size of the iterations, and set the char array to the correct size of the loop at the beginning, and then append to this?

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Ke.
  • 2,484
  • 8
  • 40
  • 78
  • What do you expect it to print ? – Shravan40 Sep 12 '16 at 16:32
  • I was hoping to store each hex value for each iteration in a long string, which I can print out at once (at the end of the loop) – Ke. Sep 12 '16 at 16:33
  • At the moment it prints one hex value for each iteration - which is expected, but I want to change it to one output at the end. – Ke. Sep 12 '16 at 16:34
  • @Ke what is the problem? Is it, how to store the values? Or how to make them hex characters? – Iharob Al Asimi Sep 12 '16 at 16:35
  • 1
    It sounds like you might want to use `sprintf()` instead: http://www.cplusplus.com/reference/cstdio/sprintf/ "Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str." – Random Davis Sep 12 '16 at 16:35
  • ahh nice, im new to this buffer pointed by str but will investigate – Ke. Sep 12 '16 at 16:36

2 Answers2

1

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

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • I'm wondering how does the 1st one work? I notice that there is an "input" how do I get this input into the main function for processing? Also, does the char array size need to be double the size, due to the \0 being added after every character? – Ke. Sep 12 '16 at 22:43
  • @Ke. `scanf("%d", &ch)` reads an integer from stdin. If the return value is 1 (i.e. success) the value read, is stored in `ch`. The array doesn't need to be double size. There is only 1 termination. Therefore array size is `N+1` so that it can hold N characters and 1 termination. – Support Ukraine Sep 13 '16 at 06:08
-1

I'm not very sure what you want to do but you can check this code I did for you and maybe extend it.

#include <stdio.h>
#include <string.h>

int main (void)
{


  char buff[2056];
  char out[255];
  char ch ='a';

  sprintf(out, "%02X", (int)(ch & 0x00FF) ); 

  strcpy(buff,out);
  printf("%s",buff);



  return 0;
}
CGeorgian
  • 551
  • 6
  • 10