public class ByteUtil
    {
        /// <summary>
        /// string >>Length
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int getLength(String str)
        {
            return getBytes(str).Length;
        }
        /// <summary>
        /// string >Encoding>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <param name="charsetName"></param>
        /// <returns></returns>
        public static byte[] getBytes(String data, String charsetName)
        {
            return System.Text.Encoding.GetEncoding(charsetName).GetBytes(data);
        }
        /// <summary>
        /// string >GBK>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(String data)
        {
            return getBytes(data, "GBK");
        }
        /// <summary>
        /// short>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(short data)
        {
            byte[] bytes = new byte[2];
            bytes[0] = (byte)(data & 0xFF);
            bytes[1] = (byte)((data & 0xFF00) >> 8);
            return bytes;
        }
        /// <summary>
        /// char>>byte[定长]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(char c, int count)
        {
            byte[] bytes = new byte[count];
            for (int i = 0; i < bytes.Length; ++i)
            {
                bytes[i] = (byte)c;
            }
            return bytes;
        }
        /// <summary>
        /// int>length==4>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(int data)
        {
            byte[] bytes = new byte[4];
            bytes[0] = (byte)(data & 0xFF);
            bytes[1] = (byte)((data & 0xFF00) >> 8);
            bytes[2] = (byte)((data & 0xFF0000) >> 16);
            bytes[3] = (byte)((data & 0xFF000000) >> 24);
            return bytes;
        }
        /// <summary>
        /// long>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(long data)
        {
            byte[] bytes = new byte[8];
            bytes[0] = (byte)(int)(data & 0xFF);
            bytes[1] = (byte)(int)(data >> 8 & 0xFF);
            bytes[2] = (byte)(int)(data >> 16 & 0xFF);
            bytes[3] = (byte)(int)(data >> 24 & 0xFF);
            bytes[4] = (byte)(int)(data >> 32 & 0xFF);
            bytes[5] = (byte)(int)(data >> 40 & 0xFF);
            bytes[6] = (byte)(int)(data >> 48 & 0xFF);
            bytes[7] = (byte)(int)(data >> 56 & 0xFF);
            return bytes;
        }
        /// <summary>
        /// float>>byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] getBytes(float data)
        {
            int intBits = BitConverter.ToInt32(BitConverter.GetBytes(data), 0);

return getBytes(intBits);
        }
        /// <summary>
        /// GB2312 byte【】==》 中文
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string LanChange(byte[] data)
        {
            Encoding utf8;
            Encoding gb2312;
            utf8 = Encoding.GetEncoding("UTF-8");
            gb2312 = Encoding.GetEncoding("GB2312");
            data = Encoding.Convert(gb2312, utf8, data);
            return utf8.GetString(data);
        }
        /// <summary>
        /// 中文==》 GB2312 byte【】
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ChangeLan(string data)
        {
            byte[] bs = Encoding.GetEncoding("UTF-8").GetBytes(data);
            bs = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GB2312"), bs);
            return bs;
        }
        /// <summary>
        /// 16进制字符串转回string
        /// </summary>
        /// <param name="hs"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string HexStringToString(string hs, Encoding encode)
        {
            string strTemp = "";
            byte[] b = new byte[hs.Length / 2];
            for (int i = 0; i < hs.Length / 2; i++)
            {
                strTemp = hs.Substring(i * 2, 2);
                if (strTemp != "\0\0")
                {
                    b[i] = Convert.ToByte(strTemp, 16);
                }
            }
            //按照指定编码将字节数组变为字符串
            return encode.GetString(b);
        }
        /// <summary>
        /// string 转回16进制字符串
        /// </summary>
        /// <param name="hs"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string StringToHexString(string s, Encoding encode)
        {
            byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
            string result = string.Empty;
            for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
            {
                result += Convert.ToString(b[i], 16);
            }
            return result;
        }
    }

各类型转换成byte[] 和HexString的更多相关文章

  1. java基本数据类型转换成byte[]数组

    import java.io.UnsupportedEncodingException;  public class ConToByte {      /**     * double转换byte   ...

  2. 聊聊java基础,int值强制类型转换成byte

    聊聊java基础,int值强制类型转换成byte 知识点:byte.short.char在表达式中会自动提升为int 之前做一个应用时,打印IP地址,因为是用4个byte存储的,所以打印的时候值范围是 ...

  3. Java byte类型转换成int类型时需要 & 0XFF的原因

    Java byte类型转换成int类型时需要 & 0XFF的原因 假设有byte b  = -1; 那么b的二进制是:1111 1111. 如果将b直接转换为int类型,那么二进制是 1111 ...

  4. Java将其他数据类型转换成JSON字符串格式

    Student.java package com.demo.servlet; import java.util.List; import java.util.Map; public class Stu ...

  5. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  6. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

  7. 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型

    问题描述: java里面时间类型转换成json数据就成这样了: "createTime":{"date":30,"day":3," ...

  8. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  9. 工具类:将其他编码类型转换成UTF-8或者其他类型的工具类

    将其他编码类型转换成UTF-8或者其他类型的工具类 public static String changeUTF(String str) { String newStr = null; try { n ...

随机推荐

  1. Eclipse 异常关闭

    缺失 Java Builder 造成运行main方法,找不到主类, 系统没有自动编译 在.project 文件中添加 <buildSpec> <buildCommand> &l ...

  2. Git(二):常用 Git 命令清单

    转: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图 ...

  3. win 10+ iis 10 部署.net core 1.1 web api

    今天上午部署了wcf,部署了好久,一直没有部署好,最后找到了dudu的部署方法,结果中午吃饭的时候成功了,这是链接:http://www.cnblogs.com/dudu/p/3328066.html ...

  4. linux命令详解2-文件管理,查看文件命令

    1. cat命令(tac) ----连接文件,并打印出来(顺序打印)与tac命令类似只不过,tac命令是逆序打印 [语法] cat [OPTION]... [FILE]... [选项] -A:显示所有 ...

  5. 不要在遍历子结点时修改parent

    [不要在遍历子结点时修改parent] 在用for/foreach遍历子结点时,如果在这过程中有改变子结点的parent,会导致不可预料的结果.我所遇到的问题是,在此种情况下,并非所有的子结点都能遍历 ...

  6. sql解决主键冲突

    在数据插入的时候,假设主键对应的值已经存在,则插入失败!这就是主键冲突.当主键存在冲突(duplicate key)的时候,可以选择性的进行处理,即忽略.更新或者替换. 1.忽略 insert ign ...

  7. 114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

  8. JS回调函数深入篇

    <有些错别字> 在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中 ...

  9. mysql5.6配置semi_sync

    测试环境:Red Hat Enterprise Linux Server release 6.3 (Santiago)Server version: 5.6.22-log MySQL Communit ...

  10. telnet客户端模拟浏览器发送请求

    telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...