📄 byteutility.java
字号:
package smsproc.util;
import java.io.UnsupportedEncodingException;
/**
* <p>Title: smsproc</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author vienna
* @version 1.0
*/
public class ByteUtility {
private static final char Digit[]={
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'
};
// static{
// logger=LogFactory.getLog(ByteUtility.class);
// }
public ByteUtility() {
}
public static byte[] strToBytes(String str)
{
return str.getBytes();
}
public static String bytesToStr(byte bytes[])
{
return (new String(bytes)).trim();
}
public static String bytesToStr(byte bytes[], String dcs)
{
try{
return (new String(bytes, dcs)).trim();
}
catch(Exception ex){
return null;
}
}
public static String bytesToStr(byte bytes[], int offset, int length)
{
return (new String(bytes, offset, length)).trim();
}
public static byte[] intToBytes(int i)
{
byte abyte[] = new byte[4];
abyte[3] = (byte)(i & 0xff);
abyte[2] = (byte)(i >> 8 & 0xff);
abyte[1] = (byte)(i >> 16 & 0xff);
abyte[0] = (byte)(i >> 24 & 0xff);
return abyte;
}
public static final int bytesToInt(byte bytes[])
{
return (bytes[3] & 0xff) + ((bytes[2] & 0xff) << 8) + ((bytes[1] & 0xff) << 16) + ((bytes[0] & 0xff) << 24);
}
public static byte[] byteToBytes(int i)
{
byte abyte[] = new byte[1];
abyte[0] = (byte)(i & 0xff);
return abyte;
}
public static final int bytesToByte(byte bytes[])
{
return bytes[0] & 0xff;
}
public static final byte[] longToBytes(long l)
{
byte abyte0[] = new byte[8];
abyte0[7] = (byte)(int)(255L & l);
abyte0[6] = (byte)(int)((65280L & l) >> 8);
abyte0[5] = (byte)(int)((0xff0000L & l) >> 16);
abyte0[4] = (byte)(int)((0xffffffffff000000L & l) >> 24);
int i = (int)(l >> 32);
abyte0[3] = (byte)(0xff & i);
abyte0[2] = (byte)((0xff00 & i) >> 8);
abyte0[1] = (byte)((0xff0000 & i) >> 16);
abyte0[0] = (byte)((0xff000000 & i) >> 24);
return abyte0;
}
public static long bytesToLong(byte abyte0[])
{
return (long)abyte0[0] << 56 & 0xff00000000000000L | (long)abyte0[1] << 48 & 0xff000000000000L | (long)abyte0[2] << 40 & 0xff0000000000L | (long)abyte0[3] << 32 & 0xff00000000L | (long)abyte0[4] << 24 & 0xff000000L | (long)abyte0[5] << 16 & 0xff0000L | (long)abyte0[6] << 8 & 65280L | (long)abyte0[7] & 255L;
}
public static String dataFormat(byte context[], int dcs)
throws UnsupportedEncodingException
{
if(dcs == 8)
return new String(context, "UTF-16BE");
if(dcs == 15)
return new String(context, "gbk");
else
return new String(context);
}
public static byte[] dataFormat(String contextStr, int dcs)
throws UnsupportedEncodingException
{
if(dcs == 8 || dcs == 24)
return contextStr.getBytes("UTF-16BE");
if(dcs == 15)
return contextStr.getBytes("gbk");
else
return contextStr.getBytes();
}
public static String byteHex(byte ib)
{
char ob[] = new char[2];
ob[0] = Digit[ib >>> 4 & 0xf];
ob[1] = Digit[ib & 0xf];
return new String(ob);
}
public static void printBytes(byte bytes[])
{
// logger.info("[bytes debug:");
String info = "";
for(int i = 0; i < bytes.length; i++)
info = info + byteHex(bytes[i]) + " ";
// logger.info(info + " bytes debug]");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -