字符串,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 ...
随机推荐
- iOS_autoLayout_Masonry
概述 Masonry是一个轻量级的布局框架与更好的包装AutoLayout语法. Masonry有它自己的布局方式,描述NSLayoutConstraints使布局代码更简洁易读. ...
- Java MVC Controller 中通过不同方式获取 @PathVariable 参数值
1.最常用,也是最直接使用方法,通过@PathVariable注解获取 @RequestMapping(value = "/test/{a}") public @ResponseB ...
- Object中的一些方法
Object.create()参数为一个对象,创建一个对象,其原型为参数,如果为null,则无原型. Object.keys()参数为对象,返回一个数组,为对象中所有可枚举的自有属性 Object.g ...
- JDBC成绩管理系统
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- Josephu--Java链表实现
public class Josephu { public static void main(String[] args) { Cyclink cyclink=new Cyclink(); cycli ...
- JBoss集群中启用HTTPS协议
Generate server certificate Note: If you already have certificate created then this section can be i ...
- C#之属性
在C#类中有属性这个成员,C#属性用来读写类的字段.实际上是通过get和set访问器实现的.
- SQLSTATE[HY000] [2003] Cant connect to MySQL server
今天要连远程数据库,结果PHP报错 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003 ...
- zipalign内存对齐优化
zipalign:android中SDK下tools文件夹 用来对资源文件的内存进行对齐优化 手工命令: 优化:zipalign -v 4 source.apk destination.apk 4: ...
- poj2240 floyd
//Accepted 732 KB 782 ms //floyd应用 #include <cstdio> #include <cstring> #include <ios ...