Scott Hanselman

Converting from a String Representation of a Unicode Character back into a char

April 18, 2005 Comment on this post [4] Posted in Internationalization
Sponsored By

Hopefully Michael Kaplan will step in here and explain some edge case or just a general comment like "that's totally wrong, Scott" - but until he does:

A fellow emailed me this question:

I want to convert a string representation of a Unicode character back into a 'char' in .NET C#.  Can you help?
 
i.e."U+0041" which is Hexidecimal for 65 which is ASCII for "A"
 
There's got to be a built in function(s) for this, and I just can't seem to find them?
 
To give you an idea, the pseudocode would be something like:
 
string s = "U+0041";
char c = new ?Unicode.Decoder.Decode?(s);
textBox1.Text = c.ToString();

Now, I have no idea why this gentleman would want to do this, but it's interesting enough. Here's what I came up with. I'm sure there's a better way.

//Just a reminder that you can use \u to escape Unicode in C#
char c = '\u0063';
Console.WriteLine(c.ToString());

//Here's how you'd go from a string to stuff like
// U+0053 U+0063 U+006f
string scott = "Scott and the letter c";
foreach(char s in scott)
{
	Console.Write("U+{0:x4} ",(int)s);
}
		
//Here's how converted a string (assuming it starts with U+)
// containing the representation of a char
// back to a char
// Is there a built in, or cleaner way? Would this work in Chinese?
string maybeC = "U+0063";
int p = int.Parse(maybeC.Substring(2), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine((char)p);

Now playing: Craig Armstrong - Ray's Theme

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Date.ParseExact and the subtle goo that is DateTime Format Strings

April 15, 2005 Comment on this post [5] Posted in Internationalization
Sponsored By

We recently needed to parse this evil. 

string poop = "2005-12-14T14:35:32.1700000-07:00";
DateTime poo = DateTime.ParseExact(poop,"yyyy-MM-ddTHH:mm:ss.fffffffzzz", System.Globalization.CultureInfo.InvariantCulture);

Important Notes:

  • HH means 24 hour time, hh means 12 hour time.
    • Don't do all your testing in the morning of a one digit month on a one digit day. 11:59pm on New Years Eve is an important test case.
    • If I'd have tested with 01:01:01 then using lower case h's would have worked, but would have broken this afternoon.
  • zzz means TimeZone offset like -07:00
  • When doing a ParseExact and then round-tripping with a ToString using the same format string will NOT result in the same string.
    • That means that:
      DateTime foo = DateTime.ParseExact(poo,"yyyy-MM-ddTHH:mm:ss.fffffffzzz", System.Globalization.CultureInfo.InvariantCulture);
      foo.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz") == "2005-12-14T13:35:32.1700000-08:00"
      However, the semantics of the DateTime is the same.
  • fffffff means 7 digits of fractional seconds
  • T (in this context) has no specific meaning (it’s not reserved) so it is treated as a literal.
  • System.Globalization.CultureInfo.InvariantCulture is almost always necessary when you’re working in a non-UI context.
    • Don't get screwed when your application doesn't work when installed on an international version of Windows. 

 

 

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

TechEd Video #4 - It's all about Community. And Ice Cream. And Baby Carrots.

April 13, 2005 Comment on this post [10] Posted in ASP.NET | TechEd | Web Services | Javascript | Speaking
Sponsored By

In the fourth and final installment of our "Those are some really weird TechEd Videos Collection (coming soon in DVD, not)" Rory and I learn the meaning of community as we sleep through the TechEd Keynote Address.

Please spread it around and Trackback/Pingback it. If you can't view it below, you can download it here.

Remember, if you're blogging TechEd this year, make sure to register your blog at TechEd Bloggers.NET and get your content aggregated!


play video stop video indicatorhandleamount downloaded toggle sound launch in external player
Launch the streaming media file

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

TopDesk: Expose Clone for Windows (and capturing video from DirectX)

April 12, 2005 Comment on this post [32] Posted in Bugs
Sponsored By

An animated GIF of TopDesk in action.Finally, my search for a decent Windows Task Switcher is over. Sure, there's TaskSwitchXP, which kicks the Windows Power Toy Task Switcher's booty.

But, no, I'm not talking about an Alt-Tab replacement. I'm talking about the pure sex that is Mac OS X Expose, but on Windows, and written in DirectX. It's TopDesk, and it's the bomb. And it's US$9.95? Sold. He sure ain't doing it for the money.

It has been around a bit, but it used OpenGL and it was pretty buggy. The author recently rewrote the whole thing in DirectX, adding a bunch of new features, including floating buttons for TabletPC support and corner hotspots.

Oh, and it supports multi-monitor, too? Awesome.

I set it up with the very corners of my monitors as "hotspots" to start the taskswitcher. Since I'm a developer, I also changed the default hotkey from the Mac default of F9 to a more reasonable Alt-F9.

Here's a Windows Media 9 encoded video of it in action on my machine. I used Fraps (another awesome util) to do the DirectX capture to AVI, FYI. Then I used RiverPast Video Cleaner to make an Animated GIF from that AVI. Then I used GifWorks.com's Online Animated GIF Editor to make it loop.

I sure love utils.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Psuedo Internationalization and your ASP.NET Application

April 09, 2005 Comment on this post [1] Posted in ASP.NET | Internationalization | Nant | ViewState | Bugs
Sponsored By

John Robbins has a great MSDN BugSlayer Article from April of 2004 on Psuedo Internationalization. When you're creating localization-ready applications, but you don't want to go to all the hassle of localizing your ever-changing resources to a specific language, you can create psuedo-internaFile Attachment: Psuedoizer.zip (11 KB)tionalized resources.

These are resources using not only funky characters (to cover more of the character spectrum), but they may be longer (simulating more 'verbose' languages like German).

For example, here's an English language snippet from one of our resource files:

<data name="Accounts.Download.Title">
  <value>Transaction Download</value>
</data>
<data name="Accounts.Statements.Action.ViewStatement">
  <value>View Statement</value>
</data>
<data name="Accounts.Statements.Instructions">
  <value>Select an account below to view or download your available online statements.</value>
</data>

Here's the same snippet Psuedo-internationalized:

  <data name="Accounts.Download.Title">
    <value>[Ŧřäʼnşäčŧįőʼn Đőŵʼnľőäđ !!! !!!]</value>
  </data>
  <data name="Accounts.Statements.Action.ViewStatement">
    <value>[Vįęŵ Ŝŧäŧęmęʼnŧ !!! !!!]</value>
  </data>
  <data name="Accounts.Statements.Instructions">
    <value>[Ŝęľęčŧ äʼn äččőūʼnŧ þęľőŵ ŧő vįęŵ őř đőŵʼnľőäđ yőūř äväįľäþľę őʼnľįʼnę şŧäŧęmęʼnŧş. !!! !!! !!! !!! !!!]</value>
  </data> 

It can still be read as near-English, which means you can localize your ASP.NET application to this funky almost-language and see:

  • Which strings in your application you missed pulling into resources
  • What you application looks like with longer strings
  • If you correctly handle the higher-order character sets

John's article includes a nice WinForms application to "psuedoize" resources. However his code doesn't take into consideration:

  • Resources that include markup like <a href={0}>. It will actually psuedoize the "a href" which will actually break your application. I've changed it to watch for the entering and exiting of < >'s and { }'s.
  • There's no command-line version.

I wanted a version to solve both these problems because I want to automatically psuedoize our applications during the Continuous Integration NAnt build. That means, Joe Developer adds a string, and the build will automatically generate psuedo-resources that include them all.

Here's my enhanced I18n Psuedoizer with much respect to John Robbin's original. Psuedoizer.zip (11.03 KB)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.