惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
Spread Privacy
Spread Privacy
GbyAI
GbyAI
F
Fortinet All Blogs
小众软件
小众软件
B
Blog RSS Feed
博客园_首页
量子位
Y
Y Combinator Blog
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
Last Week in AI
Last Week in AI
F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
S
Schneier on Security
Latest news
Latest news
I
Intezer
A
Arctic Wolf
T
Threatpost

博客园 - bluealarm

IIS7中的站点、应用程序和虚拟目录详细介绍 (转) svn提交时强制添加注释 (转) 通过IIS调试ASP.NET项目 当前标识(IIS APPPOOL\DefaultWebSite)没有对“C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files“的写访问权限 - bluealarm (转)WPF控件开源资源 visual 继承当前被禁用,因为基类引用设备特定的组件或包含 p/invoke(转) RDLC导出到EXCEL错误 C#编写客户端AcitveX控件 修改SQLServerExpress的登录模式 通过批处理文件(.bat)执行.sql文件 VS2008找不到导出模板 代码生成相关工具及技术 开源框架项目列表 已处理证书链,但是在不受信任提供程序信任的根证书中终止。 压缩数据库 清理SQL Server数据库日志的两种方法 SQL Server数据库文件恢复技术 WCF客户端链接服务超时--客户端close ASP.net中的几个概念 - bluealarm - 博客园
C# byte数组常用扩展浅析(转)
bluealarm · 2010-06-03 · via 博客园 - bluealarm

    C# byte数组常用扩展是我们编程中经常会碰到的一些实用性很强的操作,那么C# byte数组常用扩展都有哪些呢?下面将列出并用实例演示常用八种情况。

    C# byte数组常用扩展应用一:转换为十六进制字符串

    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.  } 

    第二个扩展返回的十六进制字符串是连着的,一些情况下为了阅读方便会用一个空格分开,处理比较简单,不再给出示例。

    C# byte数组常用扩展应用二:转换为Base64字符串

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

    C# byte数组常用扩展应用三:转换为基础数据类型

    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类还有很多方法(ToSingle、ToDouble、ToChar...),可以如上进行扩展。

    C# byte数组常用扩展应用四:转换为指定编码的字符串

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

    C# byte数组常用扩展应用五:Hash

    1.  
    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.   
    10.  public static byte[] Hash(this byte[] data)  
    11.  {  
    12. return Hash(data, null);  

    C# byte数组常用扩展应用六:位运算

    1.  
    2.  
    3. public static bool GetBit(this byte b, int index)  
    4. {  
    5. return (b & (1 << index)) > 0;  
    6. }  
    7.  
    8. public static byte SetBit(this byte b, int index)  
    9. {  
    10. b |= (byte)(1 << index);  
    11. return b;  
    12.  }  
    13.   
    14.  public static byte ClearBit(this byte b, int index)  
    15. {  
    16. b &= (byte)((1 << 8) - 1 - (1 << index));  
    17. return b;  
    18.  }  
    19.   
    20.  public static byte ReverseBit(this byte b, int index)  
    21.  {  
    22. b ^= (byte)(1 << index);  
    23.   return b;  
    24.  } 

    C# byte数组常用扩展应用七:保存为文件

    1.  public static void Save(this byte[] data, string path)  
    2.  {  
    3. File.WriteAllBytes(path, data);  
    4.  } 

    C# byte数组常用扩展应用八:转换为内存流

    1.  public static MemoryStream ToMemoryStream(this byte[] data)  
    2.  {  
    3. return new MemoryStream(data);  
    4.  }