Today, I encountered a problem at work. How can I convert a string type to byte []?
This problem is easy to understand. If you don't know it, it's enough for you to work for a long time. In fact, only one statement is OK:
Byte [] Bytearray = System. Text. encoding. Default. getbytes (STR );
How is it easy?
Also, convert byte [] to string:
String Str = System. Text. encoding. Default. getstring (bytearray );
In fact, there are many other useful methods in system. Text. Encoding class, such as getchars, converting from a byte [] to a char []. For details, refer to msdn.
In addition, there are other encoding methods, such as system. Text. utf8encoding class and system. Text. unicodeencoding class. Different classes can be selected based on different needs.
Char [] is a character array
Each char is a Unicode character.
Byte [] is a byte array.
Each byte is a one-byte unsigned integer.
1. the char length is 16 bits to indicate Unicode
The byte length is 8 bits to represent ASCII
2. the byte range is 0 to 255. An error occurs when the byte range is exceeded.
3. Convert Char to byte, which should be an eight-bit lower