Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert predefined base types in C #

Shulou Source: shulou.com Published: 2022-05-31 18:11:55 09月22日 Update

This article mainly introduces the relevant knowledge of how to convert the predefined basic types of C#, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to convert predefined basic types of C#. Let's take a look.

1. BitConverter converts the predefined base type to byte data (Unicode) 1, converts the value type into a byte array (Unicode): BitConverter.GetBytes () byte [] data = BitConverter.GetBytes ('ha'); Console.Write (data); / / 200 (two bytes) Console.Write (BitConverter.GetBytes ('1')); / 49 (two bytes) Console.Write (BitConverter.GetBytes / / 2 Console.Write (integer, 32 bits, 4 bytes) Console.Write (BitConverter.GetBytes (1.2)); / / 8 elements (eight bytes) Console.Write (BitConverter.GetBytes (true)); / / 1 (one byte) 2. Convert the byte array to the value type: BitConverter.ToXXX () char str = BitConverter.ToChar (new byte [] {200,84}, 0); Console.Write (str) / / Ha short sho = BitConverter.ToInt16 (new byte [] {69,0}, 0); Console.Write (sho.ToString ()); / / 693. convert the value of each element in the byte array to hexadecimal: BitConverter.ToString (byte []) string strHex = BitConverter.ToString (new byte [] {09,45,65,0,0}); Console.Write (strHex) / 09-2D-41-00-00string strHex = BitConverter.ToString (new byte [] {69,0,0,0,0}); Console.Write (strHex); / / 45-00-00-00-00

Note: related to the BitConverter.IsLittleEndian attribute, true means that the largest significant byte is at the right end of the word.

4. Examples: integers

Corresponding hexadecimal number

(Convert.ToString (*, 16))

Convert to byte array byte []

BitConverter.GetBytes (* *)

Hexadecimal representation of each byte of data

BitConverter.ToString (* *)

11:0x01 (2-bit alignment) [1-006554010004:0x1 0] 01-00-00-006554010004:0x1 0 (2-bit alignment) [4-line 0] 04-00-01-009999999993B9AC9FF:0x3B9AC9FF (2-bit alignment) [255pr 2014pr 59] FF-C9-9A-3B 2, character and ASCII code conversion:

An ASCII code occupies one byte, one of which is a check bit. A total of 27,128 characters, only suitable for all Latin alphabets.

1. Convert characters to ASCII codes: Console.Write (Convert.ToByte ('a')); / / 97Console.Write ((int)'a'); / / 97, cast Console.Write (Convert.ToByte ('1')); / / 49Console.Write ((int)'1'); / / 492,ASCII codes into characters: ASCII (97) = > 'dashes; (char) 97 codes.

Note: the Char character type in C # is Unicode, with each character accounting for 2 bytes.

3. Conversion between string and hexadecimal byte array void Main () {Console.WriteLine (StrToHex ("ha a 1 I z!")); Console.WriteLine ("B9 FE B0A1 61 B5 C4 31 CE D27A 21");} / / string conversion into hexadecimal string public string StrToHex (string mStr) {return BitConverter.ToString (ASCIIEncoding.Default.GetBytes (mStr)) .Replace ("-", "") } / / convert a hexadecimal string to a string public string HexToStr (string mHex) {mHex = mHex.Replace ("", "); if (mHex.Length)

Tags: Byte element array character type storage numeric hexadecimal base Cnotify binary string data subscript middle finger position Boolean general form knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Apple MariaDB Shulou Information