Click here to Skip to main content
15,881,173 members
Articles / General Programming / String
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jan 2012CPOL1 min read 9.9K   1
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 excellent, you are absolutely right.... Pin
Tieske824-Jan-12 7:55
professionalTieske824-Jan-12 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.