Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / string

String to byte conversion using .NET encoders (8bit 1-on-1)

5.00/5 (2 votes)
24 Jan 2012CPOL1 min read 10.5K  
The TestConversion function is not valid. The line of code data = data & Chr(n) uses the Chr function to build a string of characters used for the test. The problem is that the Chr function is itself bound to a code page. Specifically, it will utilize the code page that is in use by the...
The TestConversion function is not valid. The line of code "data = data & Chr(n)" uses the Chr function to build a string of characters used for the test. The problem is that the Chr function is itself bound to a code page. Specifically, it will utilize the code page that is in use by the current thread. (see Microsoft KB article regarding the Chr function: http://msdn.microsoft.com/en-us/library/613dxh46(v=vs.71).aspx[^]) So the contents of the "data" string are mapped to the thread's current code page, and not to the code page that is being tested.

The line "actual = System.Text.Encoding.GetEncoding(cp).GetBytes(data)" will return an array of bytes that map the characters in data to the code page being tested.

Consider the first test in the TestConversionTest function. This calls the TestConversion function for code page 37 which is the IBM EBCDIC code page. When TestConversion runs and n=78, the Chr function will return the "N" character and that will be the 78th character in the data string. But that value in EBCDIC represents the "+" character. And the "N" character in EBCDIC is 213. So when the second loop in the TestConversion function executes and n = 78, actual(n) will be the "+" character while expected(n) will be the "N" character. The function will return False at that point even though both code pages support both the "N" and the "+" characters.

The TestConversion function will almost assuredly fail for all but the system's default code page. My guess is that the Windows-1252 code page is the default code page on the system in use by the author when this was written.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)