How to printf character whose decimal is above 127

I am trying to printf a character from a file whose byte value in hex is CC. it is printing 'ffffffCC' I am expecting 'CC" only. how can I printf a anything above the ascii code?
Interpret it as an unsigned char instead of a signed char.
It is working thanks. and I spent 4 hours reading about unicode and all I need is put unsigned char. whew.
In case you still want to learn about Unicode, U+00CC is the character 'Ì'
On a unicode-compatible system, such as Linux, it's just a matter of printing it:

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio>
#include <clocale>
int main()
{
    // different ways to assign it
    wchar_t c1 = 0xcc;
    wchar_t c2 = L'\u00cc';
    wchar_t c3 = L'Ì';
    // printf
    std::setlocale(LC_ALL, "en_US.utf8");
    std::printf("U+00cc is %lc or %lc or %lc\n", c1, c2, c3);
}

online demo: http://liveworkspace.org/code/72573cb40cb913c16e594a3eaf384144

On Windows, you'll need to enable _O_WTEXT on your stdout.

(although I suspect you wanted to print the hexadecimal value of your byte, in which case Unicode is indeed irrelevant, since you're not dealing with characters at all)
Last edited on
I though I am done but one problem came up. below is my function. and also what is the memory that I dumped where my structure is pointing. the write went OK the read was OK until I read the byte '07' and it crashed immediately after executing .
readCharFromFile = (char)fgetc(fp);

Are control characters dealt differently by fgetc? what am I doing wrong?

>db /count:32 0x27fa68
0x0027FA68  cd cc bc 41 cc cc cc cc 5f 07 ce 19 51 da 3b bf  
0x0027FA78  45 23 00 00 cc cc cc cc cc cc cc cc cc cc cc cc 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Write structure to a file and return that same structure after writing
struct Test *WriteStructures(const struct Test *ptr, size_t count, FILE *fp)
{
    //Variable declaration and initialization
    int loopCntr;
    char *head = (char*)ptr; 
    unsigned char charFromStructure;
    //struct Test *beginPntr = (struct Test *)ptr; 
    //go to the entire structure in memory byte by byte and write it to a temp file
    for (loopCntr = 0; loopCntr < (int)(count * sizeof(struct Test)); loopCntr++)
    {
        fputc(charFromStructure = *head++, fp); 
        printf("%3x",charFromStructure);
    }
    printf("\n");
    rewind(fp);
    return((struct Test *)ptr);
}

//read structure to a file and return that same structure after writing
struct Test *ReadStructures(struct Test *ptr, size_t count, FILE *fp)
{ 
    //Variable declaration and initialization
    unsigned char readCharFromFile;
    char *traverserPntr = (char*)&ptr;
    int loopCntr; 
    // read a temp file byte by byte write the byte to the structure in memory  
    for (loopCntr = 0; loopCntr < (int)(count * sizeof(struct Test)); loopCntr++)
    {
        readCharFromFile = (char)fgetc(fp);
        *traverserPntr = readCharFromFile;
        traverserPntr++;
        printf("%3x",readCharFromFile);
    }
    printf("\n");
    return(ptr);
}
replace (char*)&ptr with (char*)ptr
Thank 'cubbi' it is working.
I checked the addresses of (char*)&ptr and (char*)ptr and they are not the same. I looked back on the parameters and I saw my mistake. What threw me off is it worked halfway before it crashed when reading the '07'. If you don't mind can you tell me the reason why did it crashed on the '07', I am aware that this a a control character, should my function read it anyway?

Thanks
It's not because of the character value, it's because it's the 10th byte in the struct

When writing to &ptr, you were writing to the stack frame of the function ReadStructures(), that is, to the memory area on stack where the parameters are stored.
I am guessing you're on a 32-bit system, so the first four bytes clobbered ptr, the next four bytes clobbered count, the next byte (5f) changed fp. On the next loop iteration, which would have returned 07, you attempted to call fgetc() on what used to be a FILE*, but it wasn't anymore, so you got a segfault.
I learn something everyday, I will go and trace it one instruction at a time and find where it is changing my code. Thanks again
Topic archived. No new replies allowed.