Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common extensions of byte

Shulou Source: shulou.com Published: 2022-06-01 07:06:48 10月24日 Update

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!

Tags: Commonly used applications characters strings content hexadecimal more algorithms articles good practical memory basics such as above situation data files articles methods see Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Microsoft OPPO Reno NVidia