各类型转换成byte[] 和HexString
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的更多相关文章
- java基本数据类型转换成byte[]数组
import java.io.UnsupportedEncodingException; public class ConToByte { /** * double转换byte ...
- 聊聊java基础,int值强制类型转换成byte
聊聊java基础,int值强制类型转换成byte 知识点:byte.short.char在表达式中会自动提升为int 之前做一个应用时,打印IP地址,因为是用4个byte存储的,所以打印的时候值范围是 ...
- Java byte类型转换成int类型时需要 & 0XFF的原因
Java byte类型转换成int类型时需要 & 0XFF的原因 假设有byte b = -1; 那么b的二进制是:1111 1111. 如果将b直接转换为int类型,那么二进制是 1111 ...
- Java将其他数据类型转换成JSON字符串格式
Student.java package com.demo.servlet; import java.util.List; import java.util.Map; public class Stu ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- 字符串string类型转换成DateTime或DateTime?类型
常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...
- 将java.util.Date类型转换成json时,使用JsonValueProcessor将date转换成希望的类型
问题描述: java里面时间类型转换成json数据就成这样了: "createTime":{"date":30,"day":3," ...
- String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)
学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...
- 工具类:将其他编码类型转换成UTF-8或者其他类型的工具类
将其他编码类型转换成UTF-8或者其他类型的工具类 public static String changeUTF(String str) { String newStr = null; try { n ...
随机推荐
- 「小程序JAVA实战」springboot的后台搭建(31)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维
Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...
- Spring3开发(一)
1 Ioc是什么? Ioc:Inversion of Control,控制反转,控制权从应用程序转移到框架(如Ioc容器),是框架的共有特性. 1.1 为什么需要IoC容器?IoC容器是如何演变的? ...
- MyBatis 与 Hibernate对比
- os.path.dirname( __ file __ ) 2018/6/2
os.path.dirname( __ file __ ) 2018/6/2 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py import os #该文件所在位 ...
- JAVA中List的三个子类。
JAVA中List的三个子类分别是:ArrayList,Vector,LinkList.下面就来比较一下他们的不同. ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全,效率高. Ve ...
- windows系统mysql-5.7.19官方绿色版zip包安装教程
环境: 系统环境 Windows 10 64位 mysql版本 5.7.19 一.万变不离的下载 下载页面:https://dev.mysql.com/downloads/mysql/ 点击 Down ...
- 146. LRU Cache (List, HashTable)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- php设置错误,错误记录
//设置错误级别. error_reporting(E_ALL); //显示所有错误 error_reporting(E_ALL&~E_NOTICE); //显示所有错误但不显示提示级别的 ...
- spring源码学习——spring整体架构和设计理念
Spring是在Rod Johnson的<Expert One-On-One J2EE Development and Design >的基础上衍生而来的.主要目的是通过使用基本的java ...