C # extensions: Basic 3: Common extensions of byte

Source: Internet
Author: User
This article is a basic article in the series "C # extensions and methods of use". It is relatively simple and has no technical content and does not need to be explained too much. We provide you with an idea. If you need to use it, please complete it yourself.

Application 1: convert to a hexadecimal string

1 public static string tohex (this Byte B)
2 {
3 return B. tostring ("X2 ");
4}
5
6 public static string tohex (this ienumerable <byte> bytes)
7 {
8 var sb = new stringbuilder ();
9 foreach (byte B in bytes)
10 sb. append (B. tostring ("X2 "));
11 return sb. tostring ();
12}

The hex string returned by the second extension is connected. In some cases, a space is used to separate the string for reading convenience. The processing is simple and examples are not given.

Application 2: convert to a base64 string

1 public static string tobase64string (byte [] bytes)
2 {
3 return convert. tobase64string (bytes );
4}

Application 3: Convert to basic data type

1 public static int toint (this byte [] value, int startindex)
2 {
3 return bitconverter. toint32 (value, startindex );
4}
5 public static long toint64 (this byte [] value, int startindex)
6 {
7 return bitconverter. toint64 (value, startindex );
8}

Bitconverter class also has many methods (tosingle, todouble, tochar...), which can be expanded as above.

 

Application 4: convert to a specified encoded string

1 public static string decode (this byte [] data, encoding)
2 {
3 return encoding. getstring (data );
4}

 

 

Application 5: Hash

1 // use the specified algorithm hash
2 public static byte [] Hash (this byte [] data, string hashname)
3 {
4 hashalgorithm algorithm;
5 If (string. isnullorempty (hashname) algorithm = hashalgorithm. Create ();
6 else algorithm = hashalgorithm. Create (hashname );
7 return algorithm. computehash (data );
8}
9 // use the default algorithm hash
10 public static byte [] Hash (this byte [] data)
11 {
12 Return Hash (data, null );
13}

 

APPLICATION 6: bitwise operation

1 // index starts from 0
2 // obtain whether the index is 1
3 Public static bool getbit (this Byte B, int index)
4 {
5 return (B & (1 <index)> 0;
6}
7 // set the index to 1
8 public static byte setbit (this Byte B, int index)
9 {
10 B | = (byte) (1 <index );
11 return B;
12}
13 // set the index to 0
14 public static byte clearbit (this Byte B, int index)
15 {
16 B & = (byte) (1 <8)-1-(1 <index ));
17 return B;
18}
19 // reverse the index
20 public static byte reversebit (this Byte B, int index)
21 {
22 B ^ = (byte) (1 <index );
23 return B;
24}

 

Application 7: Save As a file

1 public static void save (this byte [] data, string path)
2 {
3 file. writeallbytes (path, data );
4}

 

 

Application 8: Convert to memory stream

1 public static memorystream tomemorystream (this byte [] data)
2 {
3 return New memorystream (data );
4}

 

That's all you can think! Thank you for your attention!

There are 10 articles in the series "C # extensions and methods for great use", which will be published in the future. Please stay tuned!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.