7 Replies - 896 Views - Last Post: 01 February 2012 - 04:09 PM Rate Topic: -----

#1 kevindckr   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

^2 Character in CStrings?

Posted 01 February 2012 - 07:03 AM

I wanted to know if there is a way to add the "^2" character in a CString. I am trying to add it so I can display inches squared. Or must I define a small font and use that?
Is This A Good Question/Topic? 0
  • +

Replies To: ^2 Character in CStrings?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 07:08 AM

Do you mean a superscripted 2?

12452
Was This Post Helpful? 0
  • +
  • -

#3 kevindckr   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 07:31 AM

Yeah, that is what I was trying to say. I couldnt think of superscripted.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 07:36 AM

Sometimes just knowing the terms is half the battle.

Googling for "C++ superscript" got me lots of examples including this one.
http://stackoverflow...-console-output
Was This Post Helpful? 0
  • +
  • -

#5 kevindckr   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 07:51 AM

I cannot embed it in Microsoft Visual 2008. I am trying to do char(0xB2) but to no avail.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 08:22 AM

Let's see your code trying to do it
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 09:18 AM

Just FYI, you can't do this in standard C++ and I'm not even sure this can be done in the console. Something like this would generally require a graphic interface.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: ^2 Character in CStrings?

Posted 01 February 2012 - 04:09 PM

Basically it CAN be done if the console is configured properly to do so. However expanding past simple ASCII in the console can be tricky and is an area of much debate because what works on one computer (with its particular configuration) may or may-not work on the next. So you have "experts" giving advice on how to do it "properly" and invariable there is someone complaining that it didn't work for them.

So if you are writing an application that others will use and needs to support a wide variety of user configurations... try as you might you will never please everyone and your best bet is to work with wide-characters and hope for the best - deal with the bug reports as they come in.

If the program is just for your use or for an assignment you can just try to figure out what works for your computer and for your instructor's computer.

The Following code should work on many systems:
#include <iostream>

using namespace std;

int main() {
    cout << "E = mC" << char(253) << endl;

    return 0;
}


Here the extended ASCII char 253 (0xFD) is output. On some systems this works, on others it does not.

#include <iostream>

using namespace std;

int main() {
    wcout << L"E = mC˛" << endl;

    return 0;
}
or
#include <iostream>

using namespace std;

int main() {
    const wchar_t super2 = 0x00B2;
    wcout << L"E = mC" << super2 << endl;

    return 0;
}


on other systems the above will work by outputting wide chars.

On windows you would probably want to ensure that you had the right encoding for the console's current code page:
#define UNICODE
#include <windows.h>
#include <iostream>

using namespace std;



int main() {
    wchar_t str[] = L"E = mC˛";
    //we need a handle to the stdout
    HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    
    int consolCP = GetConsoleOutputCP();
    wcout << L"Console Code Page: " << consolCP << endl;
    int charCount = WideCharToMultiByte(consolCP, 0, str, -1, 0, 0, 0, 0);
    
    //temporary place for our MultiByte char...
    char* tempMBString = new char[charCount];
    WideCharToMultiByte( consolCP, 0, str, -1, tempMBString, charCount,  0, 0);   
    DWORD numBytesWritten;
    WriteFile(stdoutHandle, tempMBString, charCount-1, &numBytesWritten, 0);  
    
    delete[] tempMBString;
    return 0;
}


The code page that my console seems to be using on my desktop is IBM437 (which is basically the extended ASCII character set).

You can test other "code pages" by using the command: cpch in the console:
C:> cpch 65001 -- sets the "code page" to UTF8 - which is actually a multi-byte encoding not a code page thus the quotes.


If your want you can set the code page from your code using SetConsoleOutputCP(int).
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1