Common byte extensions in c #

Source: Internet
Author: User

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
Related Article

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.