字符串,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 ...
随机推荐
- Octopus系列之代码备份
代码 $.extend($.validator.messages, { required: "This field is required.", remote: "Ple ...
- css定位之绝对定位
绝对定位可以做很多事情,如广告位,弹出框,遮罩层等一些功能 css的定位方式:1.静态定位, 2.绝对定位(固定定位和绝对定位) ,3.相对定位 绝对定位会受到影响的因素有 1.属性的取值. 2.元素 ...
- Activity启动清空原任务栈
就是 启动新的activity 但是把之前所有的activity 都finish掉 而且所有的activity 都是在一个栈中 Intent intent = new Intent();intent ...
- xcode代码提示功能失效的解决方法
xcode 自动提示很好用 然而大量的工作也是让他吃不消了 结果今天提示功能给我来了个罢工 这当然是不行的 也是万能的搜索帮我解决了这个问题 方法很多 选择了简单的 xcode --> Wind ...
- RDMA的ibv_post_send() 函数
函数原型为 int ibv_post_send(struct ibv_qp *qp, struct ibv_send_wr *wr, struct ibv_send_wr **bad_wr); 其中s ...
- iOS流量精灵完结版
从一开始的激动,到现在的三期完结持续了将近三个半月时间,心态也开始变的坦然. 开发期间没有兑现自己的若言,没有写下所有的感悟和困难.我没有借口可言,唯一能说的只能说自己太懒....哈哈 总体来说流量监 ...
- tinymce 编辑器 上传图片
tinymce编辑器进行本地图片上传 首先下载tinymce.js之后 在form中添加一个<textarea>元素 给其一个id和name 然后就可以初始化编辑器了 tinymce.in ...
- 设置类型Double小数点位数
Double amount = 100;DecimalFormat dcmFmt = new DecimalFormat("0.0000"); String amountStr = ...
- hdu 2084
ps:这道题...是DP题..所以我去看了百度一些东西,才知道了什么是状态方程,状态转移方程.. 做的第一个DP题,然后TLE一次.贴上TLE的代码: #include "stdio.h&q ...
- [Java Basics] Reflection
For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.C ...