That provide us with specific instances of an

Info icon This preview shows pages 388–390. Sign up to view the full content.

View Full Document Right Arrow Icon
that provide us with specific instances of an Encoding object for a particular scheme. In this case, we’re using UTF8 and ASCII , which actually return instances of UTF8Encoding and ASCIIEncoding , respectively. Under normal circumstances, you do not need to know the actual type of these instances; you can just talk to the object returned through its Encoding base class. GetBytes returns us the byte array that corresponds to the actual in-memory represen- tation of a string, encoded using the relevant scheme. If we build and run this code, we see the following output: UTF-8 ----- 76 105 115 116 101 110 32 117 112 33 ASCII ----- 76 105 115 116 101 110 32 117 112 33 Notice that our encodings are identical in this case, just as promised. For basic Latin characters, UTF-8 and ASCII are compatible. (Unlike Notepad, the .NET UTF8Encod ing does not choose to add a BOM by default, so unless you use characters outside the 364 | Chapter 10: Strings
Image of page 388

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ASCII range this will in fact produce files that can be understood by anything that knows how to process ASCII.) Let’s make a quick change to the string we’re trying to change, and translate it into French. Replace the first line inside the Main method with Example 10-81 . Notice that we’ve got a capital E with an acute accent at the beginning. Example 10-81. Using a nonASCII character string listenUp = "Écoute-moi!"; If you don’t have a French keyboard and you’re wondering how to insert that E-acute character, there are a number of ways to do it. If you know the decimal representation of the Unicode code point, you can hold down the Alt key and type the number on the numeric keypad (and then release the Alt key). So Alt-0163 will insert the symbol for the UK currency, £ , and Alt-0201 produces É . This doesn’t work for the normal number keys, though, so if you don’t have a numeric keypad—most laptops don’t—this isn’t much help. Possibly the most fun, though, is to run the charmap.exe application. The program icon for it in the Start menu is buried pretty deeply, so it’s easier to type charmap into a command prompt, the Start Run box, or the Windows 7 Start menu search box. This is very instructive, and allows you to explore the various different character sets and (if you check the “Advanced view” box) encodings. You can see an image of it in Fig- ure 10-2 . Alternatively, you could just escape the character—the string literal "\u00C9coutez moi" will produce the same result. And this has the advantage of not requiring non- ASCII values in your source file. Visual Studio is perfectly able to edit various file en- codings, including UTF-8, so you can put non-ASCII characters in strings without having to escape them, and you can even use them in identifiers. But some text-oriented tools are not so flexible, so there may be advantages in keeping your source code purely ASCII.
Image of page 389
Image of page 390
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern