10

I want to print the value '½' in a file. I searched for the ascii value of this as Alt+(ascii Value) will give you the same. To my surprise I found 2 ascii values for this symbol in various sites. One is 171 and the other is 189.

I tried to write this symbol by using 171 and 189. Again to my surprise, if I am writing in Windows, 171 will give me this symbol. But if I am writing in UNIX, 189 will give me this symbol.

I was aware that there cant be 2 ASCII Values for a same symbol. But I got 2 valid codes for the same symbol in different OS. So can anyone tell what is the real ASCII Code for the symbol ½ ??

smci
  • 32,567
  • 20
  • 113
  • 146
user2238999
  • 121
  • 1
  • 1
  • 4
  • 7
    There is no ASCII code for that. Are you thinking of another character set? – alex Apr 03 '13 at 05:49
  • That isn't ASCII. It's ISO8859-1 / 'latin-1'. Different encodings will treat 8-bit characters differently, so how this gets treated depends on your OS and environment settings. – smci May 26 '20 at 02:59

5 Answers5

22

½ is not a character in the ASCII character set.

The values you're finding online probably differ because they're using different character sets. For example, before Unicode was invented, localized versions of Windows all used different code pages, in which the basic ASCII set was extended with some additional characters.

Now, of course, everything is (or should be) fully Unicode. Detailed Unicode information for that character (vulgar fraction one half) can be found here. Note that there are also multiple representations for the same numerical value (e.g., base 10, hex, binary, etc.).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Thanks for the response.. So does that mean that as this may be a Multibyte Character Set, it may not have a fixed ASCII Code??? – user2238999 Apr 03 '13 at 05:52
  • 3
    That's correct. You cannot rely on an "extended" (i.e., non ASCII) character having the same value (or even existing) in all multibyte character sets (MBCS). Of course, that's a bit of a non-issue, as there's no good reason to use MBCS nowadays. Compile your app as Unicode, which on Windows uses the UTF-16 representation. – Cody Gray - on strike Apr 03 '13 at 05:53
8

In Windows if you use the ALT codes, 3 digits will insert the equivalent "Code page 850" character so ALT + 171 will insert the ½ symbol

189 is the ANSI/UTF-8/WIN-1252/ISO8859-1 value for the ½ symbol. To use ALT codes for ANSI you MUST press 0 first, so ALT + 0189 inserts the ½ symbol

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
keV
  • 244
  • 3
  • 3
5

Please read the ASCII wikipedia page. You'll learn that ASCII has no "one half" character.

These days, most systems can be configured to use UTF-8 encoding (which is the "default" or at least the most commonly used encoding on the Web and on Unix systems).

UTF-8 is a variable length encoding for Unicode. So many characters or glyphs are represented by several bytes. For the ½ (officially the vulgar fraction one half unicode character) its UTF8 encoding is the two hex bytes 0Xc2 0xBD so in C notation \302\275

I am using the Linux Gnome Character Map utility gucharmap to find all that.

You might be interested in UTF-32 (a fixed length encoding using 32 bits characters, in which ½ is represented by 0x000000BD), or even UTF-16 in which many characters are 16 bits (in particular ½ is 0x00BD e.g. one 16 bit character in UTF-16), but not all. You may also be interested in wide characters i.e. the wchar_t of recent C or C++ standards (which is UTF-16 on Windows and UTF-32 on many Unix).

FWIW, Qt is using QChar as UTF-16 (Java also has UTF-16 char ...), but Gtk use UTF-8 i.e. variable length characters.

Notice that with variable length character encodings like UTF-8 getting the N-th character (which is not the N-th byte!) in a string requires to scan the string. Also, some byte combinations are not valid UTF-8.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

As others have pointed out: it's not in the ASCII table (values 0..127).

But it has a Unicode code of:

  • ½ U+00BD Vulgar Fraction One Half

It can also be put into text using the unicode U+2044 Fraction Slash:

  • where your text contains the three code points: 12
  • but it gets rendered as 1⁄2

This has the virtue of working for any fractions:

  • 1⁄2
  • 3⁄5
  • 22⁄7
  • 355⁄113
  • 355⁄113 - 1⁄3748629
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0

I am quite sure that it is indeed part of the ASCII Table:

In Windows, ensure 'NumLock' is on then try [ALT + (NumPAD)171] = ½. For ¼ use [ALT + 172]

Swift
  • 11
  • 1
  • 3
    What you think is ASCII, isn't. Unfortunately, that's quite common. If someone says something is ASCII, ask for the specification that says so or for a correction. – Tom Blodget Aug 15 '17 at 22:41