1. public class TypeConvert
  2. {
  3. . /* 字符串转byte[]
  4. 03. 这个方法转换后的结果是会多一些 48字符进来的就是代表的是0不知道为什么,但是可以只是取出指定的字符串就行了
  5. 04. */
  6. . 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;

}

  1. .
  2. . /* byte转short */
  3. . public final static short getShort(byte[] buf, boolean asc, int len) {
  4. . short r = ;
  5. . if (asc)
  6. . for (int i = len - ; i >= ; i--) {
  7. . r <<= ;
  8. . r |= (buf[i] & 0x00ff);
  9. . }
  10. . else
  11. . for (int i = ; i < len; i++) {
  12. . r <<= ;
  13. . r |= (buf[i] & 0x00ff);
  14. . }
  15. .
  16. . return r;
  17. . }
  18. .
  19. . /* B2 -> 0xB2 */
  20. . public static int stringToByte(String in, byte[] b) throws Exception {
  21. . if (b.length < in.length() / ) {
  22. . throw new Exception("byte array too small");
  23. . }
  24. .
  25. . int j=;
  26. . StringBuffer buf = new StringBuffer();
  27. . for (int i=; i<in.length(); i++, j++) {
  28. . buf.insert(, in.charAt(i));
  29. . buf.insert(, in.charAt(i+));
  30. . int t = Integer.parseInt(buf.toString(),);
  31. . System.out.println("byte hex value:" + t);
  32. . b[j] = (byte)t;
  33. . i++;
  34. . buf.delete(,);
  35. . }
  36. .
  37. . return j;
  38. . }
  39. .
  40. . /* byte to int */
  41. . public final static int getInt(byte[] buf, boolean asc, int len) {
  42. . if (buf == null) {
  43. . throw new IllegalArgumentException("byte array is null!");
  44. . }
  45. . if (len > ) {
  46. . throw new IllegalArgumentException("byte array size > 4 !");
  47. . }
  48. . int r = ;
  49. . if (asc)
  50. . for (int i = len - ; i >= ; i--) {
  51. . r <<= ;
  52. . r |= (buf[i] & 0x000000ff);
  53. . }
  54. . else
  55. . for (int i = ; i < len; i++) {
  56. . r <<= ;
  57. . r |= (buf[i] & 0x000000ff);
  58. . }
  59. . return r;
  60. . }
  61. .
  62. . /* int -> byte[] */
  63. . public static byte[] intToBytes(int num) {
  64. . byte[] b = new byte[];
  65. . for (int i = ; i < ; i++) {
  66. . b[i] = (byte) (num >>> ( - i * ));
  67. . }
  68. .
  69. . return b;
  70. . }
  71. .
  72. . /* short to byte[] */
  73. . public static byte[] shortToBytes(short num) {
  74. . byte[] b = new byte[];
  75. .
  76. . for (int i = ; i < ; i++) {
  77. . b[i] = (byte) (num >>> (i * ));
  78. . }
  79. .
  80. . return b;
  81. . }
  82. .
  83. . /* byte to String */
  84. . private static char findHex(byte b) {
  85. . int t = new Byte(b).intValue();
  86. . t = t < ? t + : t;
  87. .
  88. . if (( <= t) &&(t <= )) {
  89. . return (char)(t + '');
  90. . }
  91. .
  92. . return (char)(t-+'A');
  93. . }
  94. . public static String byteToString(byte b) {
  95. . byte high, low;
  96. . byte maskHigh = (byte)0xf0;
  97. . byte maskLow = 0x0f;
  98. .
  99. . high = (byte)((b & maskHigh) >> );
  100. . low = (byte)(b & maskLow);
  101. .
  102. . StringBuffer buf = new StringBuffer();
  103. . buf.append(findHex(high));
  104. . buf.append(findHex(low));
  105. .
  106. . return buf.toString();
  107. . }
  108. .
  109. . /* short -> byte */
  110. . public final static byte[] getBytes(short s, boolean asc) {
  111. . byte[] buf = new byte[];
  112. . if (asc) for (int i = buf.length - ; i >= ; i--) { buf[i] = (byte) (s & 0x00ff);
  113. . s >>= ;
  114. . }
  115. . else
  116. . for (int i = ; i < buf.length; i++) {
  117. . buf[i] = (byte) (s & 0x00ff);
  118. . s >>= ;
  119. . }
  120. . return buf;
  121. . }
  122. . /* int -> byte[] */
  123. . public final static byte[] getBytes(int s, boolean asc) {
  124. . byte[] buf = new byte[];
  125. . if (asc)
  126. . for (int i = buf.length - ; i >= ; i--) {
  127. . buf[i] = (byte) (s & 0x000000ff);
  128. . s >>= ;
  129. . }
  130. . else
  131. . for (int i = ; i < buf.length; i++) {
  132. . buf[i] = (byte) (s & 0x000000ff);
  133. . s >>= ;
  134. . }
  135. . return buf;
  136. . }
  137. .
  138. . /* long -> byte[] */
  139. . public final static byte[] getBytes(long s, boolean asc) {
  140. . byte[] buf = new byte[];
  141. . if (asc)
  142. . for (int i = buf.length - ; i >= ; i--) {
  143. . buf[i] = (byte) (s & 0x00000000000000ff);
  144. . s >>= ;
  145. . }
  146. . else
  147. . for (int i = ; i < buf.length; i++) {
  148. . buf[i] = (byte) (s & 0x00000000000000ff);
  149. . s >>= ;
  150. . }
  151. . return buf;
  152. . }
  153. .
  154. . /* byte[]->int */
  155. . public final static int getInt(byte[] buf, boolean asc) {
  156. . if (buf == null) {
  157. . throw new IllegalArgumentException("byte array is null!");
  158. . }
  159. . if (buf.length > ) {
  160. . throw new IllegalArgumentException("byte array size > 4 !");
  161. . }
  162. . int r = ;
  163. . if (asc)
  164. . for (int i = buf.length - ; i >= ; i--) {
  165. . r <<= ;
  166. . r |= (buf[i] & 0x000000ff);
  167. . }
  168. . else
  169. . for (int i = ; i < buf.length; i++) {
  170. . r <<= ;
  171. . r |= (buf[i] & 0x000000ff);
  172. . }
  173. . return r;
  174. . }
  175. . /* byte[] -> long */
  176. . public final static long getLong(byte[] buf, boolean asc) {
  177. . if (buf == null) {
  178. . throw new IllegalArgumentException("byte array is null!");
  179. . }
  180. . if (buf.length > ) {
  181. . throw new IllegalArgumentException("byte array size > 8 !");
  182. . }
  183. . long r = ;
  184. . if (asc)
  185. . for (int i = buf.length - ; i >= ; i--) {
  186. . r <<= ;
  187. . r |= (buf[i] & 0x00000000000000ff);
  188. . }
  189. . else
  190. . for (int i = ; i < buf.length; i++) {
  191. . r <<= ;
  192. . r |= (buf[i] & 0x00000000000000ff);
  193. . }
  194. . return r;
  195. . }
  196. .}

字符串,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. spring boot 初试,springboot入门,springboot helloworld例子

    因为项目中使用了spring boot ,之前没接触过,所以写个helloworld玩玩看,当做springboot的一个入门例子.搜索 spring boot.得到官方地址:http://proje ...

  2. iOS开发拓展篇—音频处理(音乐播放器5)

    iOS开发拓展篇—音频处理(音乐播放器5) 实现效果: 一.半透明滑块的设置 /** *拖动滑块 */ - (IBAction)panSlider:(UIPanGestureRecognizer *) ...

  3. JavaScript 语句

    JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. JavaScript 语句 JavaScript 语句是发给浏览器的命令. 这些命令的作用是 ...

  4. AngularJS拦截器

    AngularJS是通过拦截器提供了一个全局层面对响应进行处理的途径.拦截器实际是$http服务的基础中间件,用来向应用的业务流程中注入新的逻辑,其核心是服务工厂,通过向 $httpProvider. ...

  5. ajaxsearch要点1

    ajaxsearch中必须将form的class定义为pagerForm,才能在foot中submit按钮得到值

  6. Visual Studio 编译项目失败,提示找不到文件

     博客地址:http://blog.csdn.net/FoxDave 今天碰到了一个蠢问题,虽然咱们正常情况下是遇不到的,但这确实是个应该注意的地方,所以简单记录一下. Visual Studio ...

  7. 删除oracle表中的完全重复数据

    今天数据库除了个问题:项目中的一张表,数据是从另外一个系统中相同的表里弄过来的,但是可能由于昨天同事导数据导致我这张表中的数据出现了完全相同的情况(所有字段),全部是两条,需要删除相同的数据. 做法: ...

  8. HDU 5876 (大连网赛1009)(BFS + set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表 ...

  9. Bellovin_树状数组

    Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F( ...

  10. Woodbury matrix identity

    woodbury matrix identity 2014/6/20 [转载请注明出处]http://www.cnblogs.com/mashiqi http://en.wikipedia.org/w ...