Packing/unpacking/alias, mutual conversion between numeric types, ASCII and Unicode codes, conversion between numeric strings and numeric values, conversion between string and character array/byte array, various numerical types conversion between byte arrays, hexadecimal output, and some Conversion Processing of date data.
1. Packing, unpacking, and alias
INT-> int32 is a packing process, and vice versa. For example, short <-> int16, long <-> int64. The action of packing and unpacking is automatically completed by the compiler without manual intervention. To remember the relationships between these types, we use an alias ". C # is a fully object-oriented language that encapsulates a simple data type into a class through the default packing action. Int32, int16, and int64 are the corresponding class names. Some familiar simple and easy-to-remember names such as int, short, and long are called aliases of int32, int16, and int64 types. In addition to the three types, there are also common "aliases" which include the following:
Bool-> system. boolean (boolean type, its value is true or false)
Char-> system. Char (Bytes type, occupies two bytes, indicating 1 UNICODE character)
Byte-> system. byte (in bytes, 1 byte, range: 0 ~ 255)
Sbyte-> system. sbyte (in byte format, 8-bit integer, range:-128 ~ 127)
Ushort-> system. uint16 (unsigned short integer, representing a 16-bit positive integer, ranging from 0 ~ 65,535)
Uint-> system. uint32 (unsigned integer, representing a 32-bit positive integer, ranging from 0 ~ 4,294,967,295)
Ulong-> system. uint64 (unsigned long integer, representing a 64-bit positive integer, ranging from 0 ~ About 10 to the power of 20)
Short-> system. int16 (short integer, representing a 16-bit integer, range:-32,768 ~ 32,767)
INT-> system. int32 (integer, indicating a 32-bit integer in the range of-2,147,483,648 to 2,147,483,647)
Long-> system. int64 (long integer, which represents a 64-bit integer. The value ranges from-10 to 19 to 10)
Float-> system. Single (single-precision floating point, 4 bytes)
Double-> system. Double (double-precision floating point type, 8 bytes)
Example: byte A = 1; char B = 'a'; short c = 1;
Int d = 2; long e = 3; uint F = 4; bool G = true;
This. textbox1.text = "";
This. textbox1.appendtext ("Byte->" + A. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("char->" + B. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("short->" + C. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("int->" + D. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("Long->" + E. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("uint->" + F. GetType (). fullname + "\ n ");
This. textbox1.appendtext ("bool->" + G. GetType (). fullname + "\ n ");
The running result is as follows:
Byte-> system. byte
Char-> system. Char
Short-> system. int16
INT-> system. int32
Long-> system. int64
Uint-> system. uint32
Bool-> system. Boolean
2. Mutual conversion between numeric types
The value types mentioned here include byte, short, Int, long, fload, and double. According to the order, values of various types can be automatically converted backward. For example:
Byte A = 1; short B = A; int c = B;
Long d = C; float E = D; double F = E;
This. textbox1.text = "";
This. textbox1.appendtext ("Byte A =" + A. tostring () + "\ n ");
This. textbox1.appendtext ("Short B =" + B. tostring () + "\ n ");
This. textbox1.appendtext ("int c =" + C. tostring () + "\ n ");
This. textbox1.appendtext ("long d =" + D. tostring () + "\ n ");
This. textbox1.appendtext ("float E =" + E. tostring () + "\ n ");
This. textbox1.appendtext ("double F =" + F. tostring () + "\ n ");
The running result is that the values of each variable are 1. Of course, their types will not change. When the order of values is reversed, compilation fails, for example:
Int G = 1;
Short H = g;
This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
Result compilation error: "... form1.cs (118): The type" Int "cannot be implicitly converted to" short ""
At this time, if you insist on conversion, you should use the forced type conversion, which is the same as that in the C language, that is, use the statement in the form of "(type name) variable name" to forcibly convert the data. The preceding example is modified as follows:
Short G = 1;
Byte H = (byte) g;
This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
After compilation, H = 1 is output and the conversion is successful.
However, the value range of the data type must be taken into account in force conversion, for example:
Short G = 265; // 265 = 255 + 10
Byte H = (byte) g;
This. textbox1.appendtext ("H =" + H. tostring () + "\ n ");
There is no compilation error, but the running result is not h = 265, but h = 9 (low byte value of G ).
3. ASCII and Unicode characters
C # the conversion between characters and encoding is still extended using the C language-forced conversion, for example:
Char CH = 'a'; short II = 65;
This. textbox1.text = "";
This. textbox1.appendtext ("the ASCII code of \ '" + CH + "\' is:" + (short) CH + "\ n ");
This. textbox1.appendtext ("ASCII is" + II. tostring () + ", the char is:" + (char) II + "\ n ");
Char Cn = '中'; short UC = 22478;
This. textbox1.appendtext ("the Unicode of \ '" + CN + "\' is:" + (short) CN + "\ n ");
This. textbox1.appendtext ("Unicode is" + UC. tostring () + ", the char is:" + (char) UC + "\ n ");
Its running result is
The ASCII code of 'A' is: 97
ASCII is 65, the char is:
The Unicode of 'is: 20013
Unicode is 22478, the char is: City
4. Conversion between a numeric string and a numeric value
All numeric void tostring () methods can convert data into numeric strings. For example, 123. tosting () will get the string "123 ".
You can use static parse () static functions such as int and float to convert a numeric string to a numeric value. For example, float F = float. parse ("3.21"); the result F is 3.21f. Of course, other numeric types can be converted using the same method.
5. Conversion between string and character array
The void tochararray () method of the string class system. string can be used to convert strings to character arrays. For example:
Character array to string: You can use the constructor of the system. string class.
Char [] TCS = {'T', 'E','s ', 't', '', 'M', 'E '};
String tstr = new string (TCS );
This. textbox1.appendtext ("tstr = \" "+ tstr +" \ "\ n ");
Enter tstr = "test me" in the running result. The test indicates that the conversion is successful.
The [] Operator of system. string can obtain a character in the string. Example
Char CH = tstr [3];
This. textbox1.appendtext ("\" "+ tstr +" \ "[3] =" + CH. tostring ());
The correct output is "test me" [3] = T. After testing, the output is correct.
6. Conversion between strings and byte Arrays
The conversion between a string and a byte array requires another class: system. Text. encoding. This class provides the Bye [] getbytes (string) method to convert a string to a byte array, and the string getstring (byte []) method to convert a byte array to a string.
System. text. the encoding class has several default encoding: encoding. default (get the system's current ANSI code page encoding), encoding. ASCII (get the encoding of the 7-bit ASCII character set), encoding. unicode (get the encoding in the unicode format in the little-Endian byte order), encoding. utf7 (get the encoding of the UTF-7 format), encoding. utf8 (get the encoding of the UTF-8 format) and so on.
Encoding. default and encoding. UNICODE: encoding is used to convert a string to a byte array. default converts each single-byte character, such as a half-width English character, into one byte, and converts each double-byte character, such as a Chinese character, into two bytes. Encoding. Unicode converts both of them into two bytes.
For example:
String S = "C # Language ";
Byte [] b1 = system. Text. encoding. Default. getbytes (s );
Byte [] b2 = system. Text. encoding. Unicode. getbytes (s );
String T1 = "", T2 = "";
Foreach (byte B in B1 ){
T1 + = B. tostring ("") + "";
}
Foreach (byte B in B2 ){
T2 + = B. tostring ("") + "";
}
This. textbox1.text = "";
This. textbox1.appendtext ("b1.length =" + b1.length + "\ n ");
This. textbox1.appendtext (t1 + "\ n ");
This. textbox1.appendtext ("b2.length =" + b2.length + "\ n ");
This. textbox1.appendtext (T2 + "\ n ");
The running result is as follows:
B1.length = 6
67 35 211 239 209 212
B2.length = 8
67 0 35 0 237 139 0 138
Converts a byte array to a string using the string getstring (byte []) or string getstring (byte [], Int, INT) Methods of the encoding class, the encoding determines whether to use encoding. Instance:
Byte [] BS = {97, 98, 99,100,101,102 };
String Ss = system. Text. encoding. ASCII. getstring (BS );
This. textbox1.appendtext ("the string is:" + SS + "\ n ");
The running result is: the string is: abcdef
7. Conversion between various numeric types and byte Arrays
When you convert data of a numeric type to a byte array, the corresponding size of the byte array is obtained. Similarly, You need to convert the byte array to a numeric type, also, the byte array must be larger than the number of bytes of the corresponding value type. This type of conversion uses the class system. bitconverter. This class provides byte [] getbytes (...) methods To convert various numeric types into byte arrays. methods such as toint32, toint16, toint64, touint32, tosignle, and toboolean are also provided to convert byte arrays into corresponding numeric types.
8. Convert to hexadecimal
The tostring () method of the value type can convert the value into a string, but the result is in decimal format. You can use the tostring (string) method to convert to hexadecimal. String parameters are format delimiters. The hexadecimal format description is "X" or "x", which corresponds to uppercase letters respectively. For example:
Int A = 188;
This. textbox1.text = "";
This. textbox1.appendtext ("A (10) =" + A. tostring () + "\ n ");
This. textbox1.appendtext ("A (16) =" + A. tostring ("X") + "\ n ");
This. textbox1.appendtext ("A (16) =" + A. tostring ("X") + "\ n ");
The running result is as follows:
A (10) = 188
A (16) = BC
A (16) = BC
To control the length of the hexadecimal representation, you only need to write the number that represents the length after the format description is "X" or "x. For example, A. tostring ("X4 ")
To convert a string that represents the hexadecimal number to an integer, you must use the parse () method: parse (string, system. Globalization. numberstyles. The second parameter system. Globalization. numberstyles is an enumeration type, which indicates that the hexadecimal enumeration value is hexnumber. Int B = int. parse ("AB", system. Globalization. numberstyles. hexnumber), B = 171.
9. Conversion between date data and long integer data
Since various databases have different definitions and processes for date data, the definitions of date data in various languages are also different, to facilitate the conversion and growth of date data, the data is stored in the database.
Datetime is a datetime type data. It is converted to a long integer data During computation in C. Its long integer value is a number represented at an interval of January 1, 0001 milliseconds since midnight, January 1, 100. This number is called the read-only attribute of ticks (scale) corresponding to datetime type in datetime of C. Long longdate = datetime. Now. ticks; The datetime constructor also provides the construction of long integer data. For example, datetime thedate = new datetime (longdate );
10. Format date data
To output date data in a certain format, use the tostring () method of the system. datetime class and specify the format string for it. In msdn, The system. Globalization. datetimeformatinfo class provides a detailed description of the format string. Commonly used:
The date of one digit in a day in month D has no leading zero
A single-digit date in a DD month has a leading zero
The abbreviated name of a day in DDD week is defined in abbreviateddaynames.
The full name of a day in the dddd week is defined in daynames.
The month with one digit in the M month has no leading zero.
MM indicates that the month with one digit has a leading zero.
The abbreviated Mmm month name is defined in abbreviatedmonthnames.
The full name of mmmm month is defined in monthnames.
Y does not include the year of the epoch. If the year of the epoch is not included, the year without the leading zero is displayed.
YY indicates the year that does not contain the epoch. If the year of the epoch is less than 10, the year with the leading zero is displayed.
Yyyy includes the four-digit year of the epoch.
The hour in the hour 12-hour format has no leading zero number of hours.
The hour in the HH 12-hour format has a leading zero number of hours.
The hour in the 24-hour format has no leading zero number of hours.
One-digit hour in HH 24-hour format has a leading zero
M-minute one-digit minutes without leading zero
The number of minutes in one-digit mm minute has a leading zero.
S second, one-digit number of seconds without leading zero
There is a leading zero in the number of seconds in a single digit in SS seconds.
At this time, another problem occurs. What if the text to be output contains format characters? For example
Format = "Year: yyyy, month: Mm, Day: dd ";
This. textbox1.appendtext (now. tostring (Format) + "\ n ");
Output:
2ear: 2002, 4 on, 5: 08, 26a2: 26
This is not the result I want. What should I do? There is a way
Format = "\" Year \ ": yyyy, \ 'month \ ': Mm, \ 'day \': dd ";
This. textbox1.appendtext (now. tostring (Format) + "\ n ");
Check that the running result is correct:
Year: 2002, month: 08, Day: 26
We can see that you only need to enclose text information with single or double quotation marks.
What if the text information contains double quotation marks or single quotation marks? Please use your brains to solve this problem!