Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. printf and unsigned char

printf and unsigned char

Scheduled Pinned Locked Moved C / C++ / MFC
questionlinux
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leppie
    wrote on last edited by
    #1

    Hi all How can I print unsigned char's to the console? I compile with the "default unsigned char" option as this has to run on Linux, but in Windows it just spits out garbled characters. Thanks :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

    N 1 Reply Last reply
    0
    • L leppie

      Hi all How can I print unsigned char's to the console? I compile with the "default unsigned char" option as this has to run on Linux, but in Windows it just spits out garbled characters. Thanks :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      leppie wrote: How can I print unsigned char's to the console?

      unsigned char c = 'A';
      printf("%c",c);


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      L 2 Replies Last reply
      0
      • N Nish Nishant

        leppie wrote: How can I print unsigned char's to the console?

        unsigned char c = 'A';
        printf("%c",c);


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Thanks Nish, but I'm not sure that will help :confused: I'm reading Afrikaans text from a file. And Afrikaans has all those nice o's, e's, with various thingies on them. Here's some code:

        FILE *input;
        char inputfile[50] = "input.txt";
        char word[50];
        char yesno;

        if ((input = fopen(inputfile, "r")))
        {
        while (!feof(input))
        {
        fscanf(input, "%s", word);

        if (!trieIsMember(word))
        {
          //here the "upper" (128+) chars dont print properly to console
          //the char are shown properly in the debugger though, no prob in linux
          printf("Spell Error: %-20s Add word? ", word); 
          yesno = getchar();getchar(); //for enter
          if (tolower(yesno) == 'y')
            trieAdd(word);
        }
        

        }
        fclose(input);
        }

        Any ideas? I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

        N J 2 Replies Last reply
        0
        • N Nish Nishant

          leppie wrote: How can I print unsigned char's to the console?

          unsigned char c = 'A';
          printf("%c",c);


          Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          OK I found out what the problem is. It appears to be an ANSI/ASCII conflict. The letters appear fine in ANSI charset, but comes out funny in ASCII what it appears they are using for the console. Now the question is: How can I print ANSI strings properly to ASCII? And I thought DOS was dead.... ;P I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

          N 1 Reply Last reply
          0
          • L leppie

            Thanks Nish, but I'm not sure that will help :confused: I'm reading Afrikaans text from a file. And Afrikaans has all those nice o's, e's, with various thingies on them. Here's some code:

            FILE *input;
            char inputfile[50] = "input.txt";
            char word[50];
            char yesno;

            if ((input = fopen(inputfile, "r")))
            {
            while (!feof(input))
            {
            fscanf(input, "%s", word);

            if (!trieIsMember(word))
            {
              //here the "upper" (128+) chars dont print properly to console
              //the char are shown properly in the debugger though, no prob in linux
              printf("Spell Error: %-20s Add word? ", word); 
              yesno = getchar();getchar(); //for enter
              if (tolower(yesno) == 'y')
                trieAdd(word);
            }
            

            }
            fclose(input);
            }

            Any ideas? I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            leppie wrote: I'm reading Afrikaans text from a file That makes a huge difference. Now you basically need wide character versions of all the functions you have been using :-) Replace printf with wprintf, fopen with _wfopen, fscanf with fwscanf etc... Regards, Nish


            Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

            1 Reply Last reply
            0
            • L leppie

              OK I found out what the problem is. It appears to be an ANSI/ASCII conflict. The letters appear fine in ANSI charset, but comes out funny in ASCII what it appears they are using for the console. Now the question is: How can I print ANSI strings properly to ASCII? And I thought DOS was dead.... ;P I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              leppie wrote: Now the question is: How can I print ANSI strings properly to ASCII? Those are wide character strings (using unicode) See my reply to your other post :-) Nish


              Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

              L 1 Reply Last reply
              0
              • N Nish Nishant

                leppie wrote: Now the question is: How can I print ANSI strings properly to ASCII? Those are wide character strings (using unicode) See my reply to your other post :-) Nish


                Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                I can assure you those are NOT wide. :) These are the ones: ëêéô , but the command prompt just does not spit them out correctly. I get what looks like an epsilon, omega, theta and integral characters, in that order. [edit]δΩΘ⌠ , these are the ones I get on the console[/edit] Perhaps its to do with regional settings...? I will have a look. :) There is an options for codepage for non unicode programs. It was set to English US. I have change this now, but requires a reboot. I have not planned a reboot yet, but perhaps Monday will be a good day. Windows XP has been running 2wks 4days 8hrs 33mins. :eek: I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                N 1 Reply Last reply
                0
                • L leppie

                  I can assure you those are NOT wide. :) These are the ones: ëêéô , but the command prompt just does not spit them out correctly. I get what looks like an epsilon, omega, theta and integral characters, in that order. [edit]δΩΘ⌠ , these are the ones I get on the console[/edit] Perhaps its to do with regional settings...? I will have a look. :) There is an options for codepage for non unicode programs. It was set to English US. I have change this now, but requires a reboot. I have not planned a reboot yet, but perhaps Monday will be a good day. Windows XP has been running 2wks 4days 8hrs 33mins. :eek: I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #8

                  leppie wrote: I can assure you those are NOT wide. These are the ones: ëêéô Uhm, okay and sorry abt the confusion. I guess the command prompt is simply unable to show those characters by default. The codepage idea sounds good. Good luck. And if you get it to work, write an article for CP ;-) Nish


                  Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

                  L 1 Reply Last reply
                  0
                  • N Nish Nishant

                    leppie wrote: I can assure you those are NOT wide. These are the ones: ëêéô Uhm, okay and sorry abt the confusion. I guess the command prompt is simply unable to show those characters by default. The codepage idea sounds good. Good luck. And if you get it to work, write an article for CP ;-) Nish


                    Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Nishant S wrote: And if you get it to work, write an article for CP I think its rather a bug in XP ;P ë (copied) for example comes up as 235 where by defualt it should be 137 according to the ASCII table.:confused: I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                    N 1 Reply Last reply
                    0
                    • L leppie

                      Nishant S wrote: And if you get it to work, write an article for CP I think its rather a bug in XP ;P ë (copied) for example comes up as 235 where by defualt it should be 137 according to the ASCII table.:confused: I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                      N Offline
                      N Offline
                      Nirav Doshi
                      wrote on last edited by
                      #10

                      One possible way to display unsigned char in printf is to convert it to an int and display the int as a char. int x = 240; printf("%c .. %d\n", x, x); This will surely be slow for large strings! :~ Hope this helps you someway!:-O Rgds, Nirav:-) * The world stands aside to let anyone pass who knows where he is going! - David Jordan *

                      1 Reply Last reply
                      0
                      • L leppie

                        Thanks Nish, but I'm not sure that will help :confused: I'm reading Afrikaans text from a file. And Afrikaans has all those nice o's, e's, with various thingies on them. Here's some code:

                        FILE *input;
                        char inputfile[50] = "input.txt";
                        char word[50];
                        char yesno;

                        if ((input = fopen(inputfile, "r")))
                        {
                        while (!feof(input))
                        {
                        fscanf(input, "%s", word);

                        if (!trieIsMember(word))
                        {
                          //here the "upper" (128+) chars dont print properly to console
                          //the char are shown properly in the debugger though, no prob in linux
                          printf("Spell Error: %-20s Add word? ", word); 
                          yesno = getchar();getchar(); //for enter
                          if (tolower(yesno) == 'y')
                            trieAdd(word);
                        }
                        

                        }
                        fclose(input);
                        }

                        Any ideas? I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

                        J Offline
                        J Offline
                        Jambolo
                        wrote on last edited by
                        #11

                        You need to change the code page or the LOCALE or something like that.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups