Thread: ANSII codes for C++

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    ANSII codes for C++

    Hi

    1: Some days ago I heard someone saying that special characters could be implemented by something called ANSII (ya, I googled the term!). What does it really mean?

    2: Does "%" sign have some different meaning in C++ from its usual use for percentage?

    Thank you for all your help, and especially your time.

    Best wishes
    Jackson

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think the wikipedia article you are looking for is actually this one:

    ANSI escape code - Wikipedia, the free encyclopedia

    Example usage:
    Code:
    #include <iostream>
    
    #define CSI "\033["
    #define RESET CSI"0m"
    #define BOLDRED CSI"1;31m"
    #define GREEN CSI"32m"
    
    using namespace std;
    
    int main(void) {
    	cout << BOLDRED << "Hello " << GREEN << "world" << RESET << " [normal]" << endl;
    
    	return 0;
    }
    Should output:

    Hello world [normal]

    Notice that bold stays on from the first sequence until reset -- try defining GREEN as CSI"0;32m".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by stahta01 View Post
    I'm extremely sorry, stahta01. I couldn't understand your reply because of my own limitations.

    How would I enter "Pi" symbol in console based program? Suppose I want to write a line which reads, 'Enter the value "Pi symbol".r^2'.

    And what about that "%" symbol?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackson6612 View Post
    I'm extremely sorry, stahta01. I couldn't understand your reply because of my own limitations.
    ASCII are codes for various symbols. "ANSI escape sequences" can be used (eg) to control terminal colors, as shown above, on ANSI compliant devices (which your text console emulates).

    Unfortunately, pi is not part of the basic ASCII set, which is limited to 255 one byte values. Fortunately, the C++ standard allows the use of unicode characters, qv:

    List of Unicode characters - Wikipedia, the free encyclopedia

    We want the Greek small letter pi. So:

    Code:
    #include <iostream>
    
    #define BOLDMAGENTA "\033[1;35m"
    #define PI_SYMBOL "\u03c0"
    
    using namespace std;
    
    int main(void) {
    	cout << "Symbol for pi: " << BOLDMAGENTA << PI_SYMBOL << endl;
    
    	return 0;
    }
    Should yield π.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> #define PI_SYMBOL "\u03c0"
    It's only by luck that you actually see a pi symbol in your output.

    The compiler has to convert U+03C0 to the execution character set, which is implementation defined. GCC just happens to use UTF8 as the default execution character set. So "\u03C0" is replaced with "\xCF\x80". Then if your locale is also setup for UTF8, you should see the pi symbol.

    Using GCC, if you compile with "-fexec-charset=CP437" then it will be replaced with "\xE3", which is the value for the pi symbol in that character set.
    http://www.tachyonsoft.com/uc0003.htm#U03C0

    MSVC always uses the ACP for converting UCN's to char's. My ACP is 1252, which doesn't support the pi symbol.
    >> warning C4566: character represented by universal-character-name '\u03C0' cannot be represented in the current code page (1252)
    And then MSVC replaces it with "?".

    gg

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by MK27 View Post
    I think the wikipedia article you are looking for is actually this one:

    ANSI escape code - Wikipedia, the free encyclopedia

    Example usage:
    Code:
    #include <iostream>
    
    #define CSI "\033["
    #define RESET CSI"0m"
    #define BOLDRED CSI"1;31m"
    #define GREEN CSI"32m"
    
    using namespace std;
    
    int main(void) {
    	cout << BOLDRED << "Hello " << GREEN << "world" << RESET << " [normal]" << endl;
    
    	return 0;
    }
    Should output:

    Hello world [normal]

    Notice that bold stays on from the first sequence until reset -- try defining GREEN as CSI"0;32m".
    MK27, I genuinely thank you for your help. I'm a beginner (as you've already figured out) and using Dev-C++ compiler.

    Unfortunately, your both codes didn't work for me. I didn't change anything in your code except I had to delete "return 0;" and include:

    system("PAUSE");
    return EXIT_SUCCESS;


    instead at the end because when I ran the exe file, it would just close down.


    Please see the linked screenshots to check the outputs on my end:

    http://img8.imageshack.us/img8/738/mk1hv.jpg
    http://img851.imageshack.us/img851/7071/mk2f.jpg

    Once again, thanks for the help.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    http://cboard.cprogramming.com/c-pro...tml#post905299

    With Dev-C++, calling WriteConsoleW() directly will be your best option. Consider upgrading your compiler - VC++ Express is free.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DIfference between , and ;
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-21-2009, 09:46 AM
  2. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 08:47 AM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM