unicode or just ignorance? Help please

J

jalkadir

Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the
windows prompt, I get '[c:\]Niña', but if my program does this:
int main(){
std::string str("Niña");
std::cout << str << std::endl;
return 0;
}

I get [C:\]Ni±a

Can someone please, please, give me a hand understanding this
problem?!!

TIA
 
M

mlimber

jalkadir said:
Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the
windows prompt, I get '[c:\]Niña', but if my program does this:
int main(){
std::string str("Niña");
std::cout << str << std::endl;
return 0;
}

I get [C:\]Ni±a

Can someone please, please, give me a hand understanding this
problem?!!

TIA

Perhaps different fonts are being used in the two windows?

Cheers! --M
 
V

Victor Bazarov

jalkadir said:
Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the
windows prompt, I get '[c:\]Niña', but if my program does this:
int main(){
std::string str("Niña");
std::cout << str << std::endl;
return 0;
}

I get [C:\]Ni±a

Can someone please, please, give me a hand understanding this
problem?!!

I don't know what the problem is. Some conversion is applied somewhere,
but it doesn't say where and how. I think the contents of string literals
are only specified in "Basic character set", and ALT-164 is "Extended
ASCII" (if your basic set is ASCII). The behaviour is essentially
undefined unless you use the proper escape notation:

#include <vector>
#include <string>
#include <iostream>

int main(){
std::string str("Ni\xa4" "a"); // Niña
std::cout << str << std::endl;
return 0;
}

V
 
J

Jay Nabonne

Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the
windows prompt, I get '[c:\]Niña', but if my program does this:
int main(){
std::string str("Niña");

Try this:

1) At a command prompt, type:

echo Ni(ALT-164)A > foo.txt

2) Open foo.txt in your source code editor.

3) Now type the same sequence into your source code editor in the same
file. You will see two different characters.

4) Save the file back and close it.

5) If you're using Visual Studio, open the file in binary mode. Otherwise,
use Debug.

You'll see:

4e 69 a4 61 0d 0a
4e 69 f1 61 0d 0a

They're different characters...

Victor's suggestion looks like it will work.

- Jay
 
Z

Zara

jalkadir said:
Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the
windows prompt, I get '[c:\]Niña', but if my program does this:
int main(){
std::string str("Niña");
std::cout << str << std::endl;
return 0;
}

I get [C:\]Ni±a

Can someone please, please, give me a hand understanding this
problem?!!

TIA

Perhaps different fonts are being used in the two windows?

Cheers! --M

This is *Off-Topic*, as it is os-dependent, but the answer follows,
hope it helps.

I deduce from what you write that you are programming in MSWindows.

You are writing the program in some windows-based IDE, where the
charcater set is unicode and/or Windows-1252. In it, 'ñ' has the code
\xf1 and 'Ñ' has the code \xd1.

When you open a console, the character set is another really
different: it is the one that IBM devised for PC some decades ago, and
it is named somethiong like ASSCVII code page 437. In it, the codes
for 'ñ' and 'Ñ' are \xa4 and \xa5, respectively. This characetr set
may be seen at Start | All programs | Accessories | System tools |
Character set, and selecting Terminal font. Well, the exact location
and name of the tool may be slightly different, I am translating form
spanish.

So, you must use some especial means to convert from one code to the
other: either write the characters by escap seuqences, or use the MS
functions CharToOem and CharToOemBuff (serach at msdn.microsoft.com)

Saludos,

Zara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top