/**********************Copyright(c),2010-2015,hzbhd Tech.Co.,Ltd***********************/
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
public class BitOperation {
/**
* @param minute 分钟
* @param second 秒
*/
public static String toTimeFormat(int hour, int minute, int second,boolean hasHour) {
StringBuilder sb = new StringBuilder();
if (hasHour) {
if (hour != 0) {
if (hour < 10)
sb.append("0");
sb.append(hour);
sb.append(":");
} else {
sb.append("00:");
}
}
if (minute != 0) {
if (minute < 10)
sb.append("0");
sb.append(minute);
sb.append(":");
} else {
sb.append("00:");
}
if (second != 0) {
if (second < 10)
sb.append("0");
sb.append(second);
} else {
sb.append("00");
}
return sb.toString();
}
/**
* byte[]转为String字符串(UNICODE编码)
*
* @CreateDate: 2016-7-18
* @Description: description
* @param bytes 要解码的Byte数组
* @param Big true为UNICODE大端编码,false为为UNICODE小端编码
* @return
*/
public static String Byte2Unicode(byte bytes[],boolean Big)
{
StringBuffer sb = new StringBuffer("");
for(int j = 0; j < bytes.length; ){
char c ;
byte high,low;
if(Big){
high = bytes[j++];
low = bytes[j++];
}else{
low = bytes[j++];
high = bytes[j++];
}
c = (char)((low & 0x00FF) + ((high << 8)& 0xFF00));
sb.append(c);
}
return sb.toString();
}
/**
* BCD码转化成字符串
*/
public static String bcd2Str(byte[] bytes) {
char temp[] = new char[bytes.length * 2], val;
for (int i = 0; i < bytes.length; i++) {
val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
if ((val >= 0) && (val <= 9)) {
temp[i * 2] = (char) (val + '0');
} else if (val == 0x0A) {
temp[i * 2] = '*';
} else if (val == 0x0B) {
temp[i * 2] = '#';
} else if (val == 0x0F) {
break;
}
val = (char) (bytes[i] & 0x0f);
if ((val >= 0) && (val <= 9)) {
temp[i * 2 + 1] = (char) (val + '0');
} else if (val == 0x0A) {
temp[i * 2 + 1] = '*';
} else if (val == 0x0B) {
temp[i * 2 + 1] = '#';
} else if (val == 0x0F) {
break;
}
}
return new String(temp);
}
/**
* 获取Byte中某位的boolean值
*
* @CreateDate: 2016-7-18
* @Description: description
* @param :mdata 传入值
* @param mBit
* 相应位
* @return
*/
public static boolean getIntByteWithBit(int mdata, int mBit) {
if (0 == mBit) {
return ((mdata & 0x01) != 0);
} else if (1 == mBit) {
return ((mdata & 0x02) != 0);
} else if (2 == mBit) {
return ((mdata & 0x04) != 0);
} else if (3 == mBit) {
return ((mdata & 0x08) != 0);
} else if (4 == mBit) {
return ((mdata & 0x10) != 0);
} else if (5 == mBit) {
return ((mdata & 0x20) != 0);
} else if (6 == mBit) {
return ((mdata & 0x40) != 0);
} else if (7 == mBit) {
return ((mdata & 0x80) != 0);
} else {
return false;
}
}
/**
* 获取Byte中连续几位的值
*
* @CreateDate: 2016-7-18
* @Description: description
* @param :mdata 传入值
* @param startBit
* 起始位
* @param len
* 取的长度
* @return Byte中连续几位的值
*/
public static int getIntFromByteWithBit(int mdata, int startBit, int len) {
int i = mdata & 0xFF;
return (i >> startBit) & ((1 << len) - 1);
}
/**
* 设置Byte中某位boolean值
*
* @CreateDate: 2016-8-24
* @Description: description
* @param :mdata 要设置的字节
* @param :mBit 要设置的位
* @param :mVal 要设置的值
* @return :设置后的值
*/
public static int setIntByteWithBit(int mdata, int mBit, boolean mVal) {
if (mVal) {
if (0 == mBit) {
mdata = (mdata | 0x01);
} else if (1 == mBit) {
mdata = (mdata | 0x02);
} else if (2 == mBit) {
mdata = (mdata | 0x04);
} else if (3 == mBit) {
mdata = (mdata | 0x08);
} else if (4 == mBit) {
mdata = (mdata | 0x10);
} else if (5 == mBit) {
mdata = (mdata | 0x20);
} else if (6 == mBit) {
mdata = (mdata | 0x40);
} else if (7 == mBit) {
mdata = (mdata | 0x80);
} else {
}
} else {
if (0 == mBit) {
mdata = (mdata & 0xFE);
} else if (1 == mBit) {
mdata = (mdata & 0xFD);
} else if (2 == mBit) {
mdata = (mdata & 0xFB);
} else if (3 == mBit) {
mdata = (mdata & 0xF7);
} else if (4 == mBit) {
mdata = (mdata & 0xEF);
} else if (5 == mBit) {
mdata = (mdata & 0xDF);
} else if (6 == mBit) {
mdata = (mdata & 0xBF);
} else if (7 == mBit) {
mdata = (mdata & 0x7F);
} else {
}
}
return mdata;
}
public static boolean getBoolBit0(int mdata) { return ((mdata & 0x01) != 0)? true:false; }
public static boolean getBoolBit1(int mdata){
return ((mdata & 0x02) != 0)? true:false;
}
public static boolean getBoolBit2(int mdata){
return ((mdata & 0x04) != 0)? true:false;
}
public static boolean getBoolBit3(int mdata) { return ((mdata & 0x08) != 0)? true:false; }
public static boolean getBoolBit4(int mdata){
return ((mdata & 0x10) != 0)? true:false;
}
public static boolean getBoolBit5(int mdata){
return ((mdata & 0x20) != 0)? true:false;
}
public static boolean getBoolBit6(int mdata)
{
return ((mdata & 0x40) != 0)? true:false;
}
public static boolean getBoolBit7(int mdata){
return ((mdata & 0x80) != 0)? true:false;
}
//整合bit0 到 bit7
public static boolean getBoolBit( int bit,int data){
switch (bit){
case 0:
return getBoolBit0(data);
case 1:
return getBoolBit1(data);
case 2:
return getBoolBit2(data);
case 3:
return getBoolBit3(data);
case 4:
return getBoolBit4(data);
case 5:
return getBoolBit5(data);
case 6:
return getBoolBit6(data);
case 7:
return getBoolBit7(data);
}
return false;
}
/**
* 设置Byte中连续几位的值
*
* @CreateDate: 2016-9-21
* @Description: description
* @param :mdata 传入值
* @param :setDefaultValue 要设置的值
* @param startBit
* 起始位
* @param len
* 取的长度
* @return 设置后Byte的值
*/
public static int setIntFromByteWithBit(int mdata, int setValue,
int startBit, int len) {
return ((~(((1 << len) - 1) << startBit)) & mdata)
| (setValue << startBit);
}
/**
* 设置目标Byte中某位boolean值为源字节中某位的boolean值
*
* @CreateDate: 2017-4-17
* @Description: description
* @param :desdata 要设置的字节
* @param :desBit 要设置的位
* @param :srcdata 源字节
* @param :srcBit 源字节的位
* @return 设置后的值
*/
public static int setIntByteWithBitFromOtherInt(int desData, int desBit,
int srcData, int srcBit) {
return setIntByteWithBit(desData, desBit,
getIntByteWithBit(srcData, srcBit));
}
/**
* 设置目标Byte中连续几位的值为源字节中连续几位的值
*
* @CreateDate: 2017-4-17
* @Description: description
* @param :desdata 要设置的字节
* @param :desStartBit 要设置的起始位
* @param :srcdata 源字节
* @param :srcStartBit 源字节的起始位
* @param len
* 取的长度
* @return 设置后的值
*/
public static int setIntFromByteWithBitFromOtherInt(int desData,
int desStartBit, int srcData, int srcStartBit, int len) {
return setIntFromByteWithBit(desData,
getIntFromByteWithBit(srcData, srcStartBit, len), desStartBit,
len);
}
// 判断活动是否前台
public static boolean isBackground(Context context, String className) {
if (context == null || TextUtils.isEmpty(className)) {
return false;
}
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() > 0) {
ComponentName cpn = list.get(0).topActivity;
if (className.equals(cpn.getClassName())) {
return false;
}
}
return true;
}
public static String bytes2HexString(byte[] b,int len) {
String ret = "";
for (int i = 0; i < len; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
}
return ret;
}
public static String bytes2HexString(int[] b, int len) {
String ret = "";
for (int i = 0; i < len; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
}
return ret;
}
public static int setWidgetVisiable(Boolean bool) {
return bool ? View.VISIBLE : View.INVISIBLE;
}
// 合并两个byte数组
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
byte[] byte_3 = new byte[byte_1.length + byte_2.length];
System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
// 合并三个byte数组
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2,byte end) {
byte[] byte_3 ;
byte_3 = byteMerger(byte_1, byte_2);
byte_3 = byteMerger(byte_3, new byte[]{end});
return byte_3;
}
//限制数据长度
//count的值应为包括0x16以及协议头在内的要发送的byte数组的长度
public static byte[] limitDataLength(byte[] bytes, int count){
byte[] mBytes =Arrays.copyOf(bytes,count);
return mBytes;
}
//自动补零函数
//count的值应为包括0x16以及协议头在内的要发送的byte数组的长度
public static byte[] compensateZero(byte[] bytes, int count){
byte[] mBytes = new byte[count];
System.arraycopy(bytes, 0, mBytes, 0, bytes.length);
for (int i = bytes.length; i < mBytes.length; i++){
mBytes[i] = 0x00;
}
return mBytes;
}
/**
* 去除原数组指定n个位置的字节,并将剩余字节重新组成一个新的数组
* @param srcArray 原数组
* @param posRemove 指定位置
* @return
*/
public static int[] restructureArray(int[]srcArray,int[]posRemove){
int finalLen=srcArray.length-posRemove.length;
int[] newSrcArray=Arrays.copyOf(srcArray, srcArray.length);
int[] finalArray=new int[finalLen];
int finalId=0;
int validValue=0xffff;
for (int i = 0; i <srcArray.length; i++) {
for (int j = 0; j <posRemove.length; j++) {
if(posRemove[j]==i){
newSrcArray[i]=validValue;
}
}
if(newSrcArray[i]!=validValue){
finalArray[finalId]=newSrcArray[i];
finalId++;
}
}
return finalArray;
}
//四舍五入保留几位小数
public static float getRound(float data,int decRemain){
double times=Math.pow(10.0,decRemain);
return (float)(Math.round(data*times)/times);
}
//返回一个数组:从指定一个字节位置开始,遇到特定的数截止。
public static byte[] getBytesEndWithAssign(byte[] srcArray,int startPos,byte endNum){
byte[] mcuIntArrayNoByte2=new byte[srcArray.length-(2+startPos)];
byte[] newArray;
int acutalLen=0;
for (int i = 0; i < srcArray.length-(2+startPos); i++) {//从MCU数组的 2下标开始,即从data0开始
mcuIntArrayNoByte2[i] =srcArray[i+(2+startPos)];
if (mcuIntArrayNoByte2[i]!=endNum){
acutalLen++;
}
}
if(acutalLen==0){
newArray=new byte[1];
}else{
newArray=new byte[acutalLen];
}
for (int i = 0; i <newArray.length; i++) {
newArray[i]=mcuIntArrayNoByte2[i];
}
return newArray;
}
/**尚摄协议源媒体信息发送 通用函数
*
* @param srcInfoCmd 源媒体信息的头 ,尚摄协议一般为0XD2
* @param srcId 当前源对应的 ID
* @return 返回要发送的数组
*/
public static byte[] setReportHiworldSrcInfoData(byte srcInfoCmd,byte srcId){
byte[] tmp_array = new byte[15];
tmp_array[0] = (byte) 0x16;
tmp_array[1] = srcInfoCmd;
tmp_array[2] = srcId;
for (int i = 3; i < 15; i++) {
tmp_array[i] = (byte) 0x20;
}
return tmp_array;
}
/**
* 令bytes的长度为length,bytes长度大于length去掉length后的数据,小于length则以零补足。
*
* @param bytes 需要修改的byte数组
* @param length 期望的修改后的长度,该长度应包含0x16和数据帧头
* @return
*/
public static byte[] makeBytesFixedLength(byte[] bytes, int length){
byte[] fixedBytes = new byte[length];
for (int i = 0; i < fixedBytes.length; i++){
if (i >= bytes.length){
fixedBytes[i] = 0x00;
}else{
fixedBytes[i] = bytes[i];
}
}
return fixedBytes;
}
public static byte[] makeBytesFixedLength(byte[] bytes, int length, int patch){
byte[] fixedBytes = new byte[length];
for (int i = 0; i < fixedBytes.length; i++){
if (i >= bytes.length){
fixedBytes[i] = (byte)patch;
}else{
fixedBytes[i] = bytes[i];
}
}
return fixedBytes;
}
public static byte[] stringGetBytes(String str, String charsetName){
byte[] ret = new byte[0];
try {
ret = str.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ret;
}
/**
* 令value小于等于max大于等于min
* @param value 输入值
* @param min 最大值
* @param max 最小值
*/
public static int rangeNumber(int value, int min, int max) {
if (value > max) value = max;
if (value < min) value = min;
return value;
}
public static int rangeNumber(int value, int max) {
return rangeNumber(value, 0, max);
}
public static String makeMediaInfoCenteredInBytes(int fixLength, String mediaInfo) {
if (mediaInfo != null && mediaInfo.length() <= fixLength) {
int mediaInfoLength = mediaInfo.length();
int leftSpace = (fixLength - mediaInfoLength) / 2;
int rightSpace = fixLength - mediaInfoLength - leftSpace;
String newMediaInfo = "";
for (int i = 0; i < leftSpace; i++) {
newMediaInfo += " ";
}
newMediaInfo += mediaInfo;
for (int i = 0; i< rightSpace; i++){
newMediaInfo += " ";
}
return newMediaInfo;
}
return "";
}
//使用getBytes时有可能出现BOM头0xFE, 0xFF
public static byte[] exceptBOMHead(byte[] bytes){
while (true){
if (bytes.length > 2 &&
((bytes[0] == (byte)0xFE && bytes[1] == (byte)0xFF)
||(bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE))){
bytes = Arrays.copyOfRange(bytes, 2, bytes.length);
}else {
break;
}
}
return bytes;
}
public static byte[] phoneNum2UnicodeBig(byte[] numbers) {
byte[] temp = new byte[numbers.length*2];
for (int i = 0; i < numbers.length; i++) {
temp[2*i] = (byte)(0x00&0xFF);
temp[2*i+1] = (byte) (numbers[i]&0xFF);
}
return temp;
}
public static int setOneBit(int original, int bit, int value) {
return original ^ ((((original >> bit) & 1) ^ value) << bit);
}
public static int getMsbLsbResult(int MSB, int LSB) {
return ((MSB & 0xFF) << 8) | (LSB & 0xFF);
}
public static int getMsb(int data) {
int i = data & 0xFFFF;
return (i >> 8) & ((1 << 8) - 1);
}
public static int getLsb(int data) {
int i = data & 0xFFFF;
return (i >> 0) & ((1 << 8) - 1);
}
public static void knob(Context context, int whatKey, int number) {
for (int i = 0; i < number; i++) {
realKeyClick(context, whatKey);
}
}
public static int getDecimalFrom8Bit(int bit7,int bit6,int bit5,int bit4,int bit3,int bit2,int bit1,int bit0){
String key2=bit7+""+bit6+""+bit5+""+bit4+""+bit3+""+bit2+""+bit1+""+bit0;//二进制
return Integer.parseInt(key2, 2);//十进制
}
}
位运算解析工具类
于 2022-12-10 09:50:01 首次发布