public class TypeConvert
{
. /* 字符串转byte[]
03. 这个方法转换后的结果是会多一些 48字符进来的就是代表的是0不知道为什么,但是可以只是取出指定的字符串就行了
04. */
. public static byte[] hexStringToBytes(String hexString) 
{  
     if (hexString == null || hexString.equals(""))
  {         
 return null;  

}

hexString = hexString.toUpperCase();

int length = hexString.length() / 2;

char[] hexChars = hexString.toCharArray();

byte[] d = new byte[length];

for (int i = 0; i < length; i++) {

int pos = i * 2;

d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

}

return d;

}

.
. /* byte转short */
. public final static short getShort(byte[] buf, boolean asc, int len) {
. short r = ;
. if (asc)
. for (int i = len - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x00ff);
. }
. else
. for (int i = ; i < len; i++) {
. r <<= ;
. r |= (buf[i] & 0x00ff);
. }
.
. return r;
. }
.
. /* B2 -> 0xB2 */
. public static int stringToByte(String in, byte[] b) throws Exception {
. if (b.length < in.length() / ) {
. throw new Exception("byte array too small");
. }
.
. int j=;
. StringBuffer buf = new StringBuffer();
. for (int i=; i<in.length(); i++, j++) {
. buf.insert(, in.charAt(i));
. buf.insert(, in.charAt(i+));
. int t = Integer.parseInt(buf.toString(),);
. System.out.println("byte hex value:" + t);
. b[j] = (byte)t;
. i++;
. buf.delete(,);
. }
.
. return j;
. }
.
. /* byte to int */
. public final static int getInt(byte[] buf, boolean asc, int len) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (len > ) {
. throw new IllegalArgumentException("byte array size > 4 !");
. }
. int r = ;
. if (asc)
. for (int i = len - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. else
. for (int i = ; i < len; i++) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. return r;
. }
.
. /* int -> byte[] */
. public static byte[] intToBytes(int num) {
. byte[] b = new byte[];
. for (int i = ; i < ; i++) {
. b[i] = (byte) (num >>> ( - i * ));
. }
.
. return b;
. }
.
. /* short to byte[] */
. public static byte[] shortToBytes(short num) {
. byte[] b = new byte[];
.
. for (int i = ; i < ; i++) {
. b[i] = (byte) (num >>> (i * ));
. }
.
. return b;
. }
.
. /* byte to String */
. private static char findHex(byte b) {
. int t = new Byte(b).intValue();
. t = t < ? t + : t;
.
. if (( <= t) &&(t <= )) {
. return (char)(t + '');
. }
.
. return (char)(t-+'A');
. }
. public static String byteToString(byte b) {
. byte high, low;
. byte maskHigh = (byte)0xf0;
. byte maskLow = 0x0f;
.
. high = (byte)((b & maskHigh) >> );
. low = (byte)(b & maskLow);
.
. StringBuffer buf = new StringBuffer();
. buf.append(findHex(high));
. buf.append(findHex(low));
.
. return buf.toString();
. }
.
. /* short -> byte */
. public final static byte[] getBytes(short s, boolean asc) {
. byte[] buf = new byte[];
. if (asc) for (int i = buf.length - ; i >= ; i--) { buf[i] = (byte) (s & 0x00ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x00ff);
. s >>= ;
. }
. return buf;
. }
. /* int -> byte[] */
. public final static byte[] getBytes(int s, boolean asc) {
. byte[] buf = new byte[];
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. buf[i] = (byte) (s & 0x000000ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x000000ff);
. s >>= ;
. }
. return buf;
. }
.
. /* long -> byte[] */
. public final static byte[] getBytes(long s, boolean asc) {
. byte[] buf = new byte[];
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. buf[i] = (byte) (s & 0x00000000000000ff);
. s >>= ;
. }
. else
. for (int i = ; i < buf.length; i++) {
. buf[i] = (byte) (s & 0x00000000000000ff);
. s >>= ;
. }
. return buf;
. }
.
. /* byte[]->int */
. public final static int getInt(byte[] buf, boolean asc) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (buf.length > ) {
. throw new IllegalArgumentException("byte array size > 4 !");
. }
. int r = ;
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. else
. for (int i = ; i < buf.length; i++) {
. r <<= ;
. r |= (buf[i] & 0x000000ff);
. }
. return r;
. }
. /* byte[] -> long */
. public final static long getLong(byte[] buf, boolean asc) {
. if (buf == null) {
. throw new IllegalArgumentException("byte array is null!");
. }
. if (buf.length > ) {
. throw new IllegalArgumentException("byte array size > 8 !");
. }
. long r = ;
. if (asc)
. for (int i = buf.length - ; i >= ; i--) {
. r <<= ;
. r |= (buf[i] & 0x00000000000000ff);
. }
. else
. for (int i = ; i < buf.length; i++) {
. r <<= ;
. r |= (buf[i] & 0x00000000000000ff);
. }
. return r;
. }
.}

字符串,int,十六进制间转换的更多相关文章

  1. matlab学习笔记10_6 字符串与数值间的转换以及进制之间的转换

    一起来学matlab-matlab学习笔记10 10_6 字符串与数值间的转换以及进制之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...

  2. C/C++ 字符、字符串转十六进制(支持中文字符串转换)

    #include <string> // std::string #include <sstream> // std::stringstream /** * #purpose ...

  3. 如何将int整型转换成String字符串类型

    自动类型转换适用于兼容类型之间从小范围到大范围数据的转换. nt转换成String int i = 10; int b=1: System.out.pritnln(a + b); 里面靠近字符串,所以 ...

  4. 将int型数字转换成6位字符串,不足的时候,前面补0

    将int型数字转换成6位字符串,不足的时候,前面补0 方法一: int num = 123; num.ToString("000000"); 方法二: int num = 123; ...

  5. 38th 字符串与 列表间的转换

    字符串与 列表间的转换 如何利用字符串 'Life is short ,I use python'输出 :'python use I, short is Life' s = 'Life is shor ...

  6. C语言字符串和十六进制的相互转换方式

    C语言的字符串操作并不像java,Csharp那样提供直接的方法,简单粗暴.所以,在转换的时候往往费力费时,近日做项目正好用到和java程序通讯,java发送过来的数据是十六进制数字组成的字符串,解析 ...

  7. Js字符串与十六进制的相互转换

    开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...

  8. IP地址字符串与BigInteger的转换

    /**  * Copyright (c) 2010, 新浪网支付中心  *      All rights reserved.  *  * Java IP地址字符串与BigInteger的转换,  * ...

  9. 字符串、十六进制、byte数组互转

    import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...

随机推荐

  1. javascript画直线和画圆的方法(非HTML5的方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. SQL Server存储过程Return、output参数及使用技巧

    SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...

  3. ets查询:查询表中的具体一列的所有值

    比如要查询goods表中的ID这一列的所有值: P = [{#goods{upgrade='$1',_ = '_'},[],['$1']}] 要查询ID和Upgrade这两列的值: P2 = [{#g ...

  4. CPU指令系统

    CPU就是通过指令系统来操控寄存器然后实现读取数据的,所以我们必须介绍一下CPU的指令系统 如果我们知道指令的英文全称,这对我们理解指令的作用有很大帮助,所以贴出指令英文全称 接下来就是介绍一些主要的 ...

  5. 深入了解Hibernate的缓存使用

    Hibernate缓存 缓存是计算机领域的概念,它介于应用程序和永久性数据存储源(如在硬盘上的文件或者数据库)之间,其作用是降低应用程序 直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中 ...

  6. 导出带图形的数据excel表

    public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...

  7. zTree简单使用和代码结构

    1.页面使用元素代码 <input type="text" id="key" class="Side_Toput2" name=&qu ...

  8. 去除Input输入框中边框的方式

    我们在做制作注册页面时,经常会需要消除Input带来的边框,这时候我们需要用到两个属性 一个是input在非激活状态下,不现实边框,代码规则为 border:none:同时当鼠标移到input输入框上 ...

  9. Java数据结构和算法之递归

    四.递归 递归是函数调用自身的一种特殊的编程技术,其应用主要在以下几个方面:   阶乘 在java当中的基本形式是: Public  void  mothed(int n){//当满足某条件时: Mo ...

  10. pip install 出现报asciii码错误的问题

    原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码. 解决办法是: python目录 Python27\Lib\site-packages 建一个文件sit ...