Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Tech Corner (http://www.abandonia.com/vbullet/forumdisplay.php?f=23)
-   -   C code noob's corner (http://www.abandonia.com/vbullet/showthread.php?t=32006)

Mighty Midget 03-09-2013 03:26 PM

Ok, here's a good Q about ASCII codes:

If I hold down left tab and type 248, I get °
If I hold down left tab and type 0248 I get ø

I'm trying to have this program type out an ø but starting a decimal number with 0 is not going to work so any idea how I can have the program print out an ø and not a °?

Japo 03-09-2013 05:07 PM

Short answer: there's no way to print "ø" with the standard library. You're barking at the wrong door. Either you want a simple console C program, or you want to print strange characters. You can of course switch to a graphic mode (again not in the standard library) and use some font library. My advice is that you continue your C learning with interesting programs that are practical to write in C, not these pointless bells and whistles, you won't learn anything useful, let alone nowadays. Use "oe" instead etc.

Forget about "°", it's completely unrelated. Windows extended the number of characters that you can create with the numpad in that way, but that doesn't mean that the ASCII character "x" has any relationship with the character "0x". You just get sixteen times as many possible codes or characters.

Portable C (the standard library) supports ASCII characters (0-255) only--and be careful with the extended set (from 128 to the final end 255--see below why).

Nowadays there are simpler ways to get international characters as well as fancy windows and whatnot. Nobody uses the console, and when they do, they don't ask it to display funny stuff.

Back in the day MS-DOS came up with a way to print international characters on the console: the commands "MODE con codepage prepare" and "... select". Since there are many more than it's not possible to get all international characters. IIRC these commands replace the standard extended set (128-255) with country-specific characters (you select the country when you call the command, you can't have all characters at once). On the other hand, this would break the display of programs that rely on the standard extended ASCII set. Moreover, the MODE command is specific to MS-DOS; for example it doesn't come with DOSBox.

The Fifth Horseman 03-09-2013 05:13 PM

The first as ASCII 0xF8. The second is Unicode 0x00F8.
Option A, figure out a way to output unicode instead of ASCII.
Option B, figure out which codepage you should be using in order for 0xF8 to map to ø and how to switch to that codepage.

Mighty Midget 03-09-2013 06:50 PM

I have it working perfectly for both å and æ, but not for ø. ASCII for å and æ both start with a non-0 and it works. The thing is ø starts with a 0, withouth the zero it's the code for a plain o.

What I'm not getting is why å and æ are ok but ø has a "useless" code.

I wrote a program to give me the ascii values for any sign and for all but ø it worked. For ø I got the value for o.

As for the whys and all that, this is a school assignment so style and reason are irrelevant I guess.

Japo 03-09-2013 07:06 PM

2 Attachment(s)
Again, the codes that start with 0 on the Windows charmap are NOT ASCII.

http://en.wikipedia.org/wiki/ASCII
Quote:

Not to be confused with MS Windows-1252, also known as "ANSI", or other types of Extended ASCII, often just called "ASCII".

The American Standard Code for Information Interchange (ASCII) is a character-encoding scheme originally based on the English alphabet that encodes 128 specified characters
See attachments.

You'd need to change codepage--which is a platform-dependent operation when available. Honestly if I were you I'd be studying interesting algorithms instead of doing archeology in the museum of horrors.

Mighty Midget 03-09-2013 07:34 PM

If I were you, I'd see it the same way but unfortunately, this assignment is pretty well defined and is going in for review.

Japo 03-09-2013 09:34 PM

OMG, it reminds me when in college they made us program a database from scratch with binary files. Who wants to use already available and specially designed tools like SQL? :rolleyes: Some teachers seem to think that it's more educative to make a wheel with clay than to build a working bicycle with available materials and less effort.

And localization is so important for beginning programmers to learn... Or for anyone for that matter... :rolleyes:

In what platform must you do this? Windows? What compiler must you use or are you using? What libraries are you allowed to use?

The Fifth Horseman 03-09-2013 10:25 PM

Look at it this way: your software is outputting the correct code. Your shell is using a codepage where the character maps to something else than it should. The simple solution is to change the codepage such as by calling system("CHCP 1142");

Japo 03-09-2013 10:29 PM

Hmm it's been a while since I learned C... :palm: Apparently (of course) Unicode is now part of the standard library. See versions of standard functions that include the letter w, for example:

http://msdn.microsoft.com/en-us/library/wc7014hz.aspx

Wide char strings are made of wchar_t instead of char, and their literals are preceded with an L:

Code:

wprintf(L"Hell\u00f8 wørld! %s",
        L'ø' == L'\u00f8' ? L"True" : L"False");

To change the console mode from ANSI to Unicode (solving the °/ø confusion), apparently you can do this:

Code:

#include <io.h>
#include <fcntl.h>

Code:

_setmode(_fileno(stdout), _O_U16TEXT);
Make sure though that the font used by your console window does have those strange characters. It's not the case by default in my case, but you can right-click on the console window title bar and go to choose properties > font tab. Then restart your program, because the font will have transformed the strange characters to ? or normal letters before outputting them. (See how pointless all this is...!)

jonh_sabugs 03-09-2013 11:30 PM

I don't think wchar_t and unicode strings are part of standard C library, only C++. I know that compilers such as GCC do not implement wchar family for C code.


The current time is 02:26 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.