常用位操作2
来源:互联网 发布:自考网络助学平台 编辑:程序博客网 时间:2024/06/03 22:50
/**
* @author canghailan 2012-03-02 21:31
*/
public class Bytes {
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
public static String toBinaryString(byte b) {
int u = toUnsigned(b);
return new String(new char[]{
DIGITS[(u >>> 7) & 0x1],
DIGITS[(u >>> 6) & 0x1],
DIGITS[(u >>> 5) & 0x1],
DIGITS[(u >>> 4) & 0x1],
DIGITS[(u >>> 3) & 0x1],
DIGITS[(u >>> 2) & 0x1],
DIGITS[(u >>> 1) & 0x1],
DIGITS[u & 0x1]
});
}
public static String toBinaryString(byte... bytes) {
char[] buffer = new char[bytes.length * 8];
for (int i = 0, j = 0; i < bytes.length; ++i) {
int u = toUnsigned(bytes[i]);
buffer[j++] = DIGITS[(u >>> 7) & 0x1];
buffer[j++] = DIGITS[(u >>> 6) & 0x1];
buffer[j++] = DIGITS[(u >>> 5) & 0x1];
buffer[j++] = DIGITS[(u >>> 4) & 0x1];
buffer[j++] = DIGITS[(u >>> 3) & 0x1];
buffer[j++] = DIGITS[(u >>> 2) & 0x1];
buffer[j++] = DIGITS[(u >>> 1) & 0x1];
buffer[j++] = DIGITS[u & 0x1];
}
return new String(buffer);
}
public static String toHexString(byte b) {
int u = toUnsigned(b);
return new String(new char[]{
DIGITS[u >>> 4],
DIGITS[u & 0xf]
});
}
public static String toHexString(byte... bytes) {
char[] buffer = new char[bytes.length * 2];
for (int i = 0, j = 0; i < bytes.length; ++i) {
int u = toUnsigned(bytes[i]);
buffer[j++] = DIGITS[u >>> 4];
buffer[j++] = DIGITS[u & 0xf];
}
return new String(buffer);
}
private static int toUnsigned(byte b) {
return b < 0 ? b + 256 : b;
}
}
- 常用位操作2
- 常用的位操作
- 常用位操作
- 常用位操作
- java常用位操作
- 位运算常用操作总结
- 位运算常用操作总结
- 位运算常用操作总结
- 位运算常用操作总结
- 常用位操作小技巧
- 常用位域操作宏
- 常用位操作小技巧
- 常用位操作小技巧
- 位运算常用操作总结
- 2.1.常用位操作符
- 常用到位操作, 共享2个常用寄存器位操作函数
- 位操作基础篇之常用位操作技巧
- VC6 位运算常用操作总结
- 线程死锁例子
- linux小白 折腾debian8笔记1 --Debian 8 安装与初步配置
- aaa
- Change default wordpress author link
- 图片切换效果beta3纯CSS构造
- 常用位操作2
- html5学习笔记1----新增元素
- 录音转换Mp3-Lame4Android 上篇-带详细SO库编译教程
- Javascript中自定义函数与匿名函数详解
- Linux系统的磁盘分区、格式化、挂载、卸载与清空磁盘数据
- Can not find the tag library descriptor for "ueye
- Java通用对象池GenericObjectPool<T>原理总结和自定义的Jedis管理池例子
- UDP 用户数据报文协议的发送和接收示例
- 鲁能联合沃美影城打造休闲体验商业新地标