Top bit set character output

This is a discussion on Top bit set character output within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me why when using C++ with Code::Blocks if I want to output a character I have ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    96

    Top bit set character output

    Can anyone tell me why when using C++ with Code::Blocks if I want to output a character I have to use character 0171 whereas every other program I use on my computer it is character 0189.

    I am guessing it is something to do with the ISO settings but I have set Code::Blocks to use ISO 8859-1 but even in the editor if I do a <alt 0189> I get 'DC2' in white text on a black background; If I output character 0189 in a program I get a c with a vertical slash through it.

    Apologies if this is the wrong forum but it is programming related in that I want a particular output.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    This is tricky stuff.
    Make sure:

    1) You are using unicode (IE, wchar_t).
    2) Your compiler uses unicode for wchar_t (whether it's UTF-8, UTF-16, UTF-32 or anything in-between doesn't matter).
    3) Your IDE saves the file in unicode.
    4) Your compiler will interpret and compile unicode files correctly.
    5) Your output font in the console (if you're using it) will display unicode correctly.
    6) To use std::wcout, not std::cout.

    Yeah, internationalization is not easy in today's C++ world.
    I know for sure VC++ supports unicode correctly, but unfortunately I don't think the default console font does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Here is the code I am using :

    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        system ("Color f9");
        HANDLE hOut;
    
        int i;
        int x=0;
        int y=0;
    
        COORD Position;
    
        hOut=GetStdHandle (STD_OUTPUT_HANDLE);
    
        for (i=32;i<=255; i++)
        {
            Position.X=x;
            Position.Y=y;
            SetConsoleCursorPosition(hOut,Position);
            cout.width(3);
            cout << i << "  " << (unsigned char)i << flush;
    
            ++y;
    
            if(y>28)
            {
                y=0;
                x+=10;
            }
       }
    
        return 0;
    }
    The output from that is :

    Name:  output.jpg
Views: 536
Size:  78.7 KB

    Dopes that clarify what is happening here and maybe what settings to change to get the correct output I want?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Like I said, use Unicode. It's the safest and easiest way to ensure you will get correct output.
    Otherwise you have to mess with code pages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks Elysia but I am not sure exactly what you mean, I changed my editor in Code::Blocks to use UTF-7 but that makes no difference; I also tried using wcout instead of cout but the output then was :

    171 171
    172 172

    etc.

    I have looked at the compiler settings but there is no setting which appears to change the character encoding.

    FWIW I also use BASIC for programming and compile that and it works as I want it to with the correct characters being output.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Try:
    Code:
    int main()
    {
        std::wcout << L'';
    }
    Also note that BASIC uses Unicode internally (well, at least Visual Basic does).
    In C++, char usually defaults to ANSI and wchar_t usually defaults to Unicode.

    Although, how good unicode support is in gcc, I don't know...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    I tried running your code Elysia and the output was a lower case c with a vertical slash through it!

    I don't think this subject is going to get resolved very easily, it must be the encoding that my console window is using if that code gives a half character on your machine, so maybe it needs a query to microsoft about changing that encoding.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Hmmm. It might be the console window, then. It is trickier than normal windows since it uses a different codepage, and a poor font.

    EDIT:
    You can try dumping it to a file and viewing it a text utility program.
    Code:
    int main()
    {
        std::wofstream file(L"your_file");
        file << L'';
    }
    Last edited by Elysia; 10-24-2011 at 07:22 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Elysia that gives a compilation error :

    no matching function for call to 'std::basic_ofstream<wchar_t>::basic_ofstream(cons t wchar_t [10])'

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    By doing a bit of hacking about I have foiund that if you start the console window with cmd /u rather than just cmd, the <alt>0189 gives a half character; I think the /u tells it to start in unicode rather than code 850 which is the default.

    Next question then - does anybody know how to set Code::Blocks so that it starts with that setting?

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,642
    C++03 only supports "const char*" file names.

    Most of the issues you are encountering are discussed here: how do i printf("") to the console?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [EOF Problem] Extra Character In Output
    By kwikness in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2010, 03:22 PM
  2. spanish character output ansii
    By nijmegen in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2002, 03:52 PM
  3. Changing the character output.
    By InFeStEd-ArCh0n in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2002, 12:39 PM
  4. Slow character output...
    By |<4D4\/eR in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2002, 09:10 AM
  5. How do I output one character??
    By kia_1998 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 09:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21