http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3731|回复: 0

[C#语言基础] C# 16进制与字符串、字节数组,汉字之间的转换

[复制链接]
发表于 2013-3-21 17:23:33 | 显示全部楼层 |阅读模式
今天见群里有人问为什么不把类型转化的收录到基类库里,其实我认为这些是不应该收录的,不过有人问到,我发一些资源做为参考吧
方式如下
从字节转16进制
[code=csharp]byte[] b = {12,10,1,17};
  string a ="";
            for (int i = 0; i < b.Length; i++)
            {
                a +=Convert.ToString(b, 16);
            }[/code]

[code=csharp]/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] strToToHexByte(string hexString)
{
    hexString = hexString.Replace(" ", "");
    if ((hexString.Length % 2) != 0)
    hexString += " ";
    byte[] returnBytes = new byte[hexString.Length / 2];
    for (int i = 0; i < returnBytes.Length; i++)
        returnBytes = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    return returnBytes;
}

/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
    string returnStr = "";
    if (bytes != null)
    {
        for (int i = 0; i < bytes.Length; i++)
        {
            returnStr += bytes.ToString("X2");
        }
    }
    return returnStr;
}

/// <summary>
/// 从汉字转换到16进制
/// </summary>
/// <param name="s"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <param name="fenge">是否每字符用逗号分隔</param>
/// <returns></returns>
public static string ToHex(string s, string charset, bool fenge)
{
    if ((s.Length % 2) != 0)
    {
        s += " ";//空格
        //throw new ArgumentException("s is not valid chinese string!");
    }
    System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
    byte[] bytes = chs.GetBytes(s);
    string str = "";
    for (int i = 0; i < bytes.Length; i++)
    {
        str += string.Format("{0:X}", bytes);
        if (fenge && (i != bytes.Length - 1))
        {
            str += string.Format("{0}", ",");
        }
    }
    return str.ToLower();
}

///<summary>
/// 从16进制转换成汉字
/// </summary>
/// <param name="hex"></param>
/// <param name="charset">编码,如"utf-8","gb2312"</param>
/// <returns></returns>
public static string UnHex(string hex, string charset)
{
    if (hex == null)
    throw new ArgumentNullException("hex");
    hex = hex.Replace(",", "");
    hex = hex.Replace("\n", "");
    hex = hex.Replace("\\", "");
    hex = hex.Replace(" ", "");
    if (hex.Length % 2 != 0)
    {
        hex += "20";//空格
    }
    // 需要将 hex 转换成 byte 数组。
    byte[] bytes = new byte[hex.Length / 2];
    for (int i = 0; i < bytes.Length; i++)
    {
        try
        {
            // 每两个字符是一个 byte。
            bytes = byte.Parse(hex.Substring(i * 2, 2),
            System.Globalization.NumberStyles.HexNumber);
        }
        catch
        {
            // Rethrow an exception with custom message.
            throw new ArgumentException("hex is not a valid hex number!", "hex");
        }
    }
    System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
    return chs.GetString(bytes);
}[/code]


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-17 20:05

© 2014-2021

快速回复 返回顶部 返回列表