Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert predefined base types in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report