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 use BitConverter.ToUInt16 () and BitConverter.ToString () in C #

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use BitConverter.ToUInt16 () and BitConverter.ToString () in C#". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use BitConverter.ToUInt16 () and BitConverter.ToString () in C#".

First: arrange the format. For example: define a constant variable const string a = "{0pc11} {1jc10}, {2pc7}"; such a format is used to arrange the positions of three variables, the first variable occupies 5 positions, the second variable occupies 8 positions, and the third variable occupies 10 positions. Both Chinese and English can be counted as one position. For example, output Console.WriteLine on the console (a, "what do you want to be the other half in the future", "find a handsome one with a car", "then buy a pair of chess"); here is a screenshot of the test

If the definition occupies less than the characters to be entered, it is automatically incremented rather than truncated.

Second: the use of BitConverter.ToUInt16 () is to convert two bytes into unsigned integers. For example, the hexadecimal of 20556 bytes is CD 38, then the conversion to unsigned integers should be reversed, that is, the conversion of 38CD to unsigned decimal integers is 14541.

Third: the use of BitConverter.ToString (), which converts a byte or byte array into a hexadecimal or hexadecimal string, concatenated with "-"

Here is the complete code for this example:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BitConverter data transformation {class Program {/ / arrangement format, the first variable occupies five positions, the second variable occupies 17 positions, and the third variable occupies 10 positions const string formatter = "{0mem5} {1mem17} {2magin10}"; / / Convert two byte array elements to a ushort and display it. Public static void BAToUInt16 (byte [] bytes, int index) {/ / BitConverter is used to convert the underlying data to and from the byte array / / BitConverter.ToUInt16 () method converts two bytes from the specified position of the byte array to an unsigned integer ushort value = BitConverter.ToUInt16 (bytes, index) / / BitConverter.ToString () byte array converted to hexadecimal string form Console.WriteLine (formatter, index, BitConverter.ToString (bytes, index, 2), value);} static void Main (string [] args) {byte [] byteArray = {15,0,0,255,3,16,39,255,255,127} Console.WriteLine ("This example of the BitConverter.ToUInt16 (byte []," + "int)\ nmethod generates the following output. It "+" converts elements\ nof a byte array to ushort values.\ n "); Console.WriteLine (" initial byte array "); Console.WriteLine ("-"); Console.WriteLine (BitConverter.ToString (byteArray)); Console.WriteLine (); Console.WriteLine (formatter," index "," array elements "," ushort ") Console.WriteLine (formatter, "-") / / Convert byte array elements to ushort values. BAToUInt16 (byteArray, 1); BAToUInt16 (byteArray, 0); BAToUInt16 (byteArray, 3); BAToUInt16 (byteArray, 5); BAToUInt16 (byteArray, 8); BAToUInt16 (byteArray, 7); Console.ReadKey ();} above are all the contents of the article "how to use BitConverter.ToUInt16 () and BitConverter.ToString () in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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