常用数据类型转换
来源:互联网 发布:神话刷号软件 编辑:程序博客网 时间:2024/06/13 22:20
/**
* switch IP Address
*
* @param int
* @return String
*/
public String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}
/**
* bytes[] to hexString
*
* @param bytes[]
* @return String(Hex)
*/
public String toHexString(byte[] bytes) {
String hexSSID = "";
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
hexSSID = hexSSID + hex;
}
return hexSSID;
}
/**
*short to byte[]
*
* @param short
* @return byte[]
*/
public static byte[] getBytes(short data){
byte[] bytes = new byte[2];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
return bytes;
}
/**
*char to byte[]
*
* @param char
* @return byte[]
*/
public static byte[] getBytes(char data){
byte[] bytes = new byte[2];
bytes[0] = (byte) (data);
bytes[1] = (byte) (data >> 8);
return bytes;
}
/**
*int to byte[]
*
* @param int
* @return byte[]
*/
public static byte[] getBytes(int data){
byte[] bytes = new byte[4];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
bytes[2] = (byte) ((data & 0xff0000) >> 16);
bytes[3] = (byte) ((data & 0xff000000) >> 24);
return bytes;
}
/**
*long to byte[]
*
* @param long
* @return byte[]
*/
public static byte[] getBytes(long data){
byte[] bytes = new byte[8];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data >> 8) & 0xff);
bytes[2] = (byte) ((data >> 16) & 0xff);
bytes[3] = (byte) ((data >> 24) & 0xff);
bytes[4] = (byte) ((data >> 32) & 0xff);
bytes[5] = (byte) ((data >> 40) & 0xff);
bytes[6] = (byte) ((data >> 48) & 0xff);
bytes[7] = (byte) ((data >> 56) & 0xff);
return bytes;
}
/**
*float to byte[]
*
* @param float
* @return byte[]
*/
public static byte[] getBytes(float data){
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}
/**
*double to byte[]
*
* @param double
* @return byte[]
*/
public static byte[] getBytes(double data){
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}
/**
*double to byte[]
*
* @param string,string(编码格式)
* @return byte[]
*/
public static byte[] getBytes(String data, String charsetName){
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
/**
*byte[] to short
*
* @param byte[]
* @return short
*/
public static short getShort(byte[] bytes){
return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
*byte[] to char
*
* @param byte[]
* @return char
*/
public static char getChar(byte[] bytes){
return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
*byte[] to int
*
* @param byte[]
* @return int
*/
public static int getInt(byte[] bytes){
return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
}
/**
*byte[] to long
*
* @param byte[]
* @return long
*/
public static long getLong(byte[] bytes){
return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
| (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
}
/**
*byte[] to float
*
* @param byte[]
* @return float
*/
public static float getFloat(byte[] bytes){
return Float.intBitsToFloat(getInt(bytes));
}
/**
*byte[] to double
*
* @param byte[]
* @return double
*/
public static double getDouble(byte[] bytes){
long l = getLong(bytes);
System.out.println(l);
return Double.longBitsToDouble(l);
}
/**
*byte[] to string
*
* @param byte[],string(编码格式)
* @return string
*/
public static String getString(byte[] bytes, String charsetName){
return new String(bytes, Charset.forName(charsetName));
}
* switch IP Address
*
* @param int
* @return String
*/
public String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}
/**
* bytes[] to hexString
*
* @param bytes[]
* @return String(Hex)
*/
public String toHexString(byte[] bytes) {
String hexSSID = "";
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
hexSSID = hexSSID + hex;
}
return hexSSID;
}
/**
*short to byte[]
*
* @param short
* @return byte[]
*/
public static byte[] getBytes(short data){
byte[] bytes = new byte[2];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
return bytes;
}
/**
*char to byte[]
*
* @param char
* @return byte[]
*/
public static byte[] getBytes(char data){
byte[] bytes = new byte[2];
bytes[0] = (byte) (data);
bytes[1] = (byte) (data >> 8);
return bytes;
}
/**
*int to byte[]
*
* @param int
* @return byte[]
*/
public static byte[] getBytes(int data){
byte[] bytes = new byte[4];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
bytes[2] = (byte) ((data & 0xff0000) >> 16);
bytes[3] = (byte) ((data & 0xff000000) >> 24);
return bytes;
}
/**
*long to byte[]
*
* @param long
* @return byte[]
*/
public static byte[] getBytes(long data){
byte[] bytes = new byte[8];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data >> 8) & 0xff);
bytes[2] = (byte) ((data >> 16) & 0xff);
bytes[3] = (byte) ((data >> 24) & 0xff);
bytes[4] = (byte) ((data >> 32) & 0xff);
bytes[5] = (byte) ((data >> 40) & 0xff);
bytes[6] = (byte) ((data >> 48) & 0xff);
bytes[7] = (byte) ((data >> 56) & 0xff);
return bytes;
}
/**
*float to byte[]
*
* @param float
* @return byte[]
*/
public static byte[] getBytes(float data){
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}
/**
*double to byte[]
*
* @param double
* @return byte[]
*/
public static byte[] getBytes(double data){
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}
/**
*double to byte[]
*
* @param string,string(编码格式)
* @return byte[]
*/
public static byte[] getBytes(String data, String charsetName){
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
/**
*byte[] to short
*
* @param byte[]
* @return short
*/
public static short getShort(byte[] bytes){
return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
*byte[] to char
*
* @param byte[]
* @return char
*/
public static char getChar(byte[] bytes){
return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
*byte[] to int
*
* @param byte[]
* @return int
*/
public static int getInt(byte[] bytes){
return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
}
/**
*byte[] to long
*
* @param byte[]
* @return long
*/
public static long getLong(byte[] bytes){
return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
| (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
}
/**
*byte[] to float
*
* @param byte[]
* @return float
*/
public static float getFloat(byte[] bytes){
return Float.intBitsToFloat(getInt(bytes));
}
/**
*byte[] to double
*
* @param byte[]
* @return double
*/
public static double getDouble(byte[] bytes){
long l = getLong(bytes);
System.out.println(l);
return Double.longBitsToDouble(l);
}
/**
*byte[] to string
*
* @param byte[],string(编码格式)
* @return string
*/
public static String getString(byte[] bytes, String charsetName){
return new String(bytes, Charset.forName(charsetName));
}
0 0
- VC常用数据类型转换
- VC常用数据类型转换
- VC常用数据类型转换
- VC常用数据类型转换
- VC常用数据类型转换
- 常用数据类型的转换
- vc++常用数据类型转换
- vc常用数据类型转换
- 常用数据类型转换
- VC常用数据类型转换
- Windows常用数据类型转换
- VC常用数据类型转换
- VC常用数据类型转换
- VC常用数据类型转换
- C++常用数据类型转换
- 常用数据类型转换(转)
- jni 常用数据类型转换
- 常用的数据类型转换
- Android RelativeLayout中layout_marginBottom失效
- Thinking in Java:并发
- org.hibernate.hql.ast.QuerySyntaxException: XX is not mapped [from XX]
- 继承与派生
- css之top,right,bottom,left
- 常用数据类型转换
- Oracle 审计功能
- eclipse插件安装时安装不上
- 解决维信小程序“URL域名不合法,请在mp后台配置后重试”
- 论文阅读——人脸质量评估
- 在Linux中开机自动运行普通用户脚本程序
- DMA技术
- 关于Spark的Broadcast解析
- 浏览器判断和移动端的判断