字符串,int,十六进制间转换
- 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,十六进制间转换的更多相关文章
- matlab学习笔记10_6 字符串与数值间的转换以及进制之间的转换
一起来学matlab-matlab学习笔记10 10_6 字符串与数值间的转换以及进制之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...
- C/C++ 字符、字符串转十六进制(支持中文字符串转换)
#include <string> // std::string #include <sstream> // std::stringstream /** * #purpose ...
- 如何将int整型转换成String字符串类型
自动类型转换适用于兼容类型之间从小范围到大范围数据的转换. nt转换成String int i = 10; int b=1: System.out.pritnln(a + b); 里面靠近字符串,所以 ...
- 将int型数字转换成6位字符串,不足的时候,前面补0
将int型数字转换成6位字符串,不足的时候,前面补0 方法一: int num = 123; num.ToString("000000"); 方法二: int num = 123; ...
- 38th 字符串与 列表间的转换
字符串与 列表间的转换 如何利用字符串 'Life is short ,I use python'输出 :'python use I, short is Life' s = 'Life is shor ...
- C语言字符串和十六进制的相互转换方式
C语言的字符串操作并不像java,Csharp那样提供直接的方法,简单粗暴.所以,在转换的时候往往费力费时,近日做项目正好用到和java程序通讯,java发送过来的数据是十六进制数字组成的字符串,解析 ...
- Js字符串与十六进制的相互转换
开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...
- IP地址字符串与BigInteger的转换
/** * Copyright (c) 2010, 新浪网支付中心 * All rights reserved. * * Java IP地址字符串与BigInteger的转换, * ...
- 字符串、十六进制、byte数组互转
import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...
随机推荐
- spring boot 初试,springboot入门,springboot helloworld例子
因为项目中使用了spring boot ,之前没接触过,所以写个helloworld玩玩看,当做springboot的一个入门例子.搜索 spring boot.得到官方地址:http://proje ...
- iOS开发拓展篇—音频处理(音乐播放器5)
iOS开发拓展篇—音频处理(音乐播放器5) 实现效果: 一.半透明滑块的设置 /** *拖动滑块 */ - (IBAction)panSlider:(UIPanGestureRecognizer *) ...
- JavaScript 语句
JavaScript 语句 JavaScript 语句向浏览器发出的命令.语句的作用是告诉浏览器该做什么. JavaScript 语句 JavaScript 语句是发给浏览器的命令. 这些命令的作用是 ...
- AngularJS拦截器
AngularJS是通过拦截器提供了一个全局层面对响应进行处理的途径.拦截器实际是$http服务的基础中间件,用来向应用的业务流程中注入新的逻辑,其核心是服务工厂,通过向 $httpProvider. ...
- ajaxsearch要点1
ajaxsearch中必须将form的class定义为pagerForm,才能在foot中submit按钮得到值
- Visual Studio 编译项目失败,提示找不到文件
博客地址:http://blog.csdn.net/FoxDave 今天碰到了一个蠢问题,虽然咱们正常情况下是遇不到的,但这确实是个应该注意的地方,所以简单记录一下. Visual Studio ...
- 删除oracle表中的完全重复数据
今天数据库除了个问题:项目中的一张表,数据是从另外一个系统中相同的表里弄过来的,但是可能由于昨天同事导数据导致我这张表中的数据出现了完全相同的情况(所有字段),全部是两条,需要删除相同的数据. 做法: ...
- HDU 5876 (大连网赛1009)(BFS + set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表 ...
- Bellovin_树状数组
Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F( ...
- Woodbury matrix identity
woodbury matrix identity 2014/6/20 [转载请注明出处]http://www.cnblogs.com/mashiqi http://en.wikipedia.org/w ...