各类型转换成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 ...
随机推荐
- 安装Apache2
Linux下安装Apache 2.4 2012-08-06 09:36:51| 分类: linux|字号 订阅 本文原创,欢迎转载.转载请在文章明显可见处张贴如下内容:(注意:请保留超链接格 ...
- UNITY UI字体模糊的原因
根本原因:像素少. 解决办法:字体的 font size将像素设置大些,然后用scale来缩放大小
- 网卡流量监控脚本 ( Shell )
#!/bin/bash # Traffic Monitor # author: Xiao Guaishou get_traffic_info(){ recv=`cat /proc/net/dev | ...
- IO操作中的建议
程序输出信息使用PrintStream(或者PrintWriter),程序输入信息使用Scaner
- elasticsearch中的mapping简介
默认mapping elasticsearch(以下简称ES)是没有模式(schema)的,当我们执行以下命令: curl -d '{"name":"zach" ...
- C#发送和接受POST请求
1.发送Post请求代码 /// <summary> /// 发起Http请求 /// </summary> /// <param name="flightDa ...
- Hadoop HA 机制学习
一.Hadoop 系统架构 1.1 Hadoop1.x和Hadoop2.x 架构 在介绍HA之前,我们先来看下Hadoop的系统架构,这对于理解HA是至关重要的.Hadoop 1.x之前,其官方架构如 ...
- java核心知识点----创建线程的第三种方式 Callable 和 Future CompletionService
前面已经指出通过实现Runnable时,Thread类的作用就是将run()方法包装成线程执行体,那么是否可以直接把任意方法都包装成线程执行体呢?Java目前不行,但其模仿者C#中是可以的. Call ...
- Texture Format全解析
[Texture Format全解析] What internal representation is used for the texture. This is a tradeoff between ...
- $(window).load()和$(document).ready()
一.前言 我们在编写前端代码的js文件时,往往是会先写一个$(function(){}),然后才会在大括号里面继续写我们自己的代码.当时并不能理解为什么要添加这样一个东西,只是把它当做一个标签一样添加 ...