In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "analysis of common extensions of byte arrays under C#". In the actual operation process of cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
C#byte array common extension application 1: conversion to hexadecimal string
public static string ToHex(this byte b) { return b.ToString("X2"); } public static string ToHex(this IEnumerable 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 contiguous. In some cases, it will be separated by a space for reading convenience. The processing is relatively simple, and no more examples are given.
C#byte array common extension application 2: conversion to Base64 string
public static string ToBase64String(byte[] bytes) { return Convert.ToBase64String(bytes); }
C#byte array common extension application three: conversion to the underlying data type
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); }
BitConverter class also has many methods (ToSingle, ToDouble, ToChar...), It can be extended as above.
C#byte array commonly used extension application four: conversion to the specified encoded string
public static string Decode(this byte[] data, Encoding encoding) { return encoding.GetString(data); }
C#byte array commonly used extension application five: Hash
//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 default hash algorithm public static byte[] Hash(this byte[] data) { return Hash(data, null); }
C#byte array commonly used extensions apply six: bit operations
//index starts from 0//get if index is 1 public static bool GetBit(this byte b, int index) { return (b & (1 < 0;)} //set index bit to 1 public static byte SetBit(this byte b, int index) { b| = (byte)(1
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.