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

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
I
InfoQ
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
B
Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
V
Visual Studio Blog

博客园 - 妖居

ASP.NET MVC Tips #2 - 令人混乱的Get、Post、Return View和Return Redirect ASP.NET MVC Tips #1 - 支持上传文件的ModelBinder How to migrate MsSql database to MySql Windows Workflow Foundation 使用小例 使用异步委托解决Windows Application应用Duplex Service时出现的Deadlock问题 表格化固定长、CSV文件编辑器工具 iMatrixitor 发布 Getting Started With LINQ in Visual Basic (翻译 + 评论) 使用接口实现附带插件功能的程序 两个简单方法加速DataGridView 使用.NET自带的功能制作简单的注册码 不是说“Peek 不会更改 StreamReader 的当前位置”么。MS骗人的! 《Introducing Visual Basic 2005》中看到的一些VB2005的新特性 VB.NET函数的返回值问题(从CSDN论坛一个问题想到的) Add-in and Automation Development In VB.NET 2003 (Finished) Add-in and Automation Development In VB.NET 2003 (8) 模拟IE地址栏的TextBox小控件 Add-in and Automation Development in VB.NET 2003 (6-7) 在WinXP环境下显示XP风格的控件 Add-in and Automation Development In VB.NET 2003 (5)
字节数组、数值和十六进制字符串的转换
妖居 · 2007-06-15 · via 博客园 - 妖居

1、   将字节数组转化为数值
public static int ConvertBytesToInt(byte[] arrByte, int offset)
{
    return BitConverter.ToInt32(arrByte, offset);
}

2、   将数值转化为字节数组
第二个参数设置是不是需要把得到的字节数组反转,因为Windows操作系统中整形的高低位是反转转之后保存的。
public static byte[] ConvertIntToBytes(int value, bool reverse)
{
    byte[] ret = BitConverter.GetBytes(value);
    if (reverse)
        Array.Reverse(ret);
    return ret;
}

3、   将字节数组转化为16进制字符串
第二个参数的含义同上。
public static string ConvertBytesToHex(byte[] arrByte, bool reverse)
{
    StringBuilder sb = new StringBuilder();
    if (reverse)
        Array.Reverse(arrByte);
    foreach (byte b in arrByte)
        sb.AppendFormat("{0:x2}", b);
    return sb.ToString();
}

4、   16进制字符串转化为字节数组
public static byte[] ConvertHexToBytes(string value)
{
    int len = value.Length / 2;
    byte[] ret = new byte[len];
    for (int i = 0; i < len; i++)
        ret[i]=(byte)(Convert.ToInt32(value.Substring(i * 2, 2), 16));
    return ret;
}