2

I am trying to reading in a file that contains characters in UTF-8 format.

    FILE * f = fopen(argv[1], "r");
    if(f == NULL){
            printf("cannot open %s\n", argv[1]);
            exit(-1);
        }
    unsigned int c = getc();
    while(c != EOF){
        printf("%d\n", c);  // UB
        c = getchar();

    }

How do I read the files in so that they are in bit representation? For example that it would look like: 0xA3. Right now it's printing actual integers. Basically, how do I read in characters in bit wise format?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Joe Caraccio
  • 1,899
  • 3
  • 24
  • 41

1 Answers1

2

sample Example for utf-8 file reading.....

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
        setlocale(LC_ALL, "en_US.UTF-8");
        FILE * f = fopen(argv[1], "r");
        if(f == NULL){
                printf("cannot open %s\n", argv[1]);
                exit(-1);
        }
        wchar_t wc;
        while((wc=fgetwc(f))!=WEOF){
                //wprintf(L"%lc", wc);
                // for output as hex of uthf-8 characters
                wprintf(L"0X%X,",(wint_t) wc);
        }
        wprintf(L"\n");
        fclose(f);
        return 0;
}
rajesh6115
  • 705
  • 9
  • 21