In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you the content of an example analysis of coding problems in C # development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
System.Text provides an abstract class of Encoding, which provides methods for string encoding. The most commonly used coding method is ASCII,Unicode,UTF8 (a kind of Unicode coding).
Unicode has four encoding formats, UTF-8 and UTF-16,UTF-32,UTF-7.
Character encoding class, ASCIIEncoding, UTF7Encoding,UnicodeEncoding,UTF32Encoding.
The following is a comparison between ASCII and Unicode coding. No nonsense, but code first:
This is ASCII encoding and decoding.
Static void Main (string [] args) {string temp = "Hello World!"; Console.WriteLine ("Original String: {0}", temp); byte [] tempBytes = System.Text.Encoding.ASCII.GetBytes (temp); Console.WriteLine ("Bytes Array: {0}", BitConverter.ToString (tempBytes)); BigInteger integer = new BigInteger (tempBytes); Console.WriteLine ("BigInteger: {0}", integer); string res = System.Text.Encoding.ASCII.GetString (tempBytes) Console.WriteLine ("Convert Back String: {0}" res); Console.ReadKey ();}
The running results are as follows:
Original String:Hello Worldwide Bytes Array:48-65-6C-6C-6F-20-57-6F-72-6C-64-21BigInteger:10334410032597741434076685640Convert Back String:Hello World!
It's normal, right? But what if the input string is in Chinese (or some other character that is not in the ASCII table)?
Change the above program code
String temp = "Hello, World!"
The running results are as follows:
Original String: Hello, World! Bytes Array:3F-3F-3F-3F-3F-3FBigInteger:69540876599103Convert Back String:?
If you change the coding format to UTF8, you will still repeat the above testing process.
Static void Main (string [] args) {string temp = "Hello, World!" ; Console.WriteLine ("Original String: {0}", temp); byte [] tempBytes = System.Text.Encoding.UTF8.GetBytes (temp); Console.WriteLine ("Bytes Array: {0}", BitConverter.ToString (tempBytes)); BigInteger integer = new BigInteger (tempBytes); Console.WriteLine ("BigInteger: {0}", integer); string res = System.Text.Encoding.UTF8.GetString (tempBytes); Console.WriteLine ("Convert Back String: {0}", res); Console.ReadKey ();}
The running results are as follows:
Original String: Hello, World! Bytes Array:E4-BD-A0-E5-A5-BD-EF-BC-8C-E4-B8-96-E7-95-8C-EF-BC-81BigInteger:-10998968812899434720462615123889939386679836Convert Back String: Hello, World! Original String:Hello Worldwide Bytes Array:48-65-6C-6C-6F-20-57-6F-72-6C-64-21BigInteger:10334410032597741434076685640Convert Back String:Hello World!
By comparison, we find that there doesn't seem to be much difference except for compatibility between Chinese and other languages. If you change the coding set to Unicode, the difference between Chinese and English character encodings can be easily seen.
Original String:Hello wordstones Bytes Array:48-00-65-00-6C-00-6C-00-6F-00-20-00-57-00-6F-00-72-00-6C-00-64-00-21-00BigInteger:3160918205608148134863399242437668999277801104545742920Convert Back String:Hello Worldwide original String: Hello, world! Bytes Array:60-4F-7D-59-0C-FF-16-4E-4C-75-01-FFBigInteger:-307722159543719876182061216Convert Back String: Hello, World!
If you don't consider other circumstances. By comparing the results, we find that:
1. ASCII can only handle English and English symbols. For details, please refer to the ASCII character table.
2. Unicode can handle all language symbols in the world.
3. When dealing with English, Unicode will add a byte 0x00 after each byte, which is twice the length of ASCII; when dealing with Chinese, the code is shorter.
4. UTF8 takes longer to process Chinese than Unicode code, and the same as ASCII when dealing with English.
In conclusion, because the storage medium is becoming less and less valuable, when dealing with non-English characters, the encoding format should choose Unicode (or any encoding format of its subset UTF8, etc.), and ASCII encoding should be selected only when it is determined that the program can only process English.
Thank you for reading! This is the end of this article on "sample Analysis of coding problems in C# Development". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.