In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the common extensions of byte. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Byte common extension application 1: convert to hexadecimal string
Public static string ToHex (this byte b) {return b.ToString ("X2");} public static string ToHex (this IEnumerable
< byte>Bytes) {var sb = new StringBuilder (); foreach (byte b in bytes) sb.Append (b.ToString ("X2")); return sb.ToString ();}
The hexadecimal string returned by the second extension is concatenated, and in some cases it will be separated by a space for ease of reading, so it is relatively simple to deal with, and the example is no longer shown.
Byte commonly used extension application II: convert to Base64 string
Public static string ToBase64String (byte [] bytes) {return Convert.ToBase64String (bytes);}
Common extension applications of byte III: conversion to basic data types
Public static int ToInt (this byte [] value, int startIndex) {return BitConverter.ToInt32 (value, startIndex);} public static long ToInt64 (this byte [] value, int startIndex) {return BitConverter.ToInt64 (value, startIndex);}
The BitConverter class also has many methods (ToSingle, ToDouble, ToChar...) that can be extended as above.
Byte common extension application 4: convert to a specified encoded string
Public static string Decode (this byte [] data, Encoding encoding) {return encoding.GetString (data);}
Common extended applications of byte 5: Hash
/ use the specified algorithm Hash public static byte [] Hash (this byte [] data, string hashName) {HashAlgorithm algorithm; if (string.IsNullOrEmpty (hashName)) algorithm = HashAlgorithm.Create (); else algorithm = HashAlgorithm.Create (hashName); return algorithm.ComputeHash (data);} / use the default algorithm Hash public static byte [] Hash (this byte [] data) {return Hash (data, null);}
Six common extended applications of byte: bit operation
/ / index starts at 0 / / gets whether the index is 1 public static bool GetBit (this byte b, int index) {return (b & (1)
< < index)) >0;} / / set the index to 1 public static byte SetBit (this byte b, int index) {b | = (byte) (1 < < index); return b;} / / set the index to 0 public static byte ClearBit (this byte b, int index) {b & = (byte) ((1 < 8)-1-(1 < < index)); return b } / / reverse the index bit to public static byte ReverseBit (this byte b, int index) {b ^ = (byte) (1 < < index); return b;}
Byte commonly used extension application 7: save as a file
Public static void Save (this byte [] data, string path) {File.WriteAllBytes (path, data);}
Common extended applications of byte: converting to memory streams
Public static MemoryStream ToMemoryStream (this byte [] data) {return new MemoryStream (data);} Thank you for reading! This is the end of this article on "what are the common extensions of byte?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.