Character Encoding and Testing

Starting last week, I have been teaching a class to prepare software test engineers for a Certificate in Software Testing with Microsoft Technologies program. The certificate program covers 42 specific topics that were listed by test managers as those topics that testers should have at least a basic understanding of. One of those 42 topics is character encoding. Yesterday morning I decided to make a tiny demo program to use to illustrate the topic. I recalled that one of my colleagues, William Rollison, is an expert at character encoding. I visited his blog and found a tool he wrote, and I decided to (approximately) reverse engineer that tool so that I could have a simple application with source code to distribute to students in the software testing certificate program. The screenshot below shows the result.
Interestingly, the character encoding logic was simple (in part because I’ve investigated the topic before) but I spent an hour wrestling with the ListView data control to display results. The demo is written with C#. Here’s the code:
private void button1_Click(object sender, EventArgs e)
  int charCount = 0;
  string s = textBox1.Text;
  char[] chars = s.ToCharArray();
  foreach (char c in chars)
    char[] arrayWithOneChar = new char[] { c };
    byte[] bytes = null;
    if (radioButton1.Checked) // ASCII
      bytes = Encoding.ASCII.GetBytes(arrayWithOneChar);
    else if (radioButton2.Checked)
      bytes = Encoding.Unicode.GetBytes(arrayWithOneChar); // Little Endian
    else if (radioButton3.Checked)
      bytes = Encoding.BigEndianUnicode.GetBytes(arrayWithOneChar);
    else if (radioButton4.Checked)
      bytes = Encoding.UTF7.GetBytes(arrayWithOneChar);
    else if (radioButton5.Checked)
      bytes = Encoding.UTF8.GetBytes(arrayWithOneChar);
    else if (radioButton6.Checked)
      bytes = Encoding.UTF32.GetBytes(arrayWithOneChar);
    string bytesForACharInHex = "";
    string bytesForACharInDecimal = "";
    foreach (byte b in bytes)
      bytesForACharInHex += b.ToString("X2") + " ";
      bytesForACharInDecimal += b.ToString() + " ";
    listView1.Items.Add(new ListViewItem(new string[] { charCount.ToString(), c.ToString(), bytesForACharInHex, bytesForACharInDecimal }));
  } // next char
} // button1_Click()
This entry was posted in Software Test Automation. Bookmark the permalink.