var NumberUtil={
//byte数组转换为int整数
bytesToInt2:function(bytes, off) {
var b3 = bytes[off] & 0xFF;
var b2 = bytes[off + 1] & 0xFF;
var b1 = bytes[off + 2] & 0xFF;
var b0 = bytes[off + 3] & 0xFF;
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
},
//byte数组转换为无符号short整数
byte2ToUnsignedShort:function(bytes, off) {
var high = bytes[off + 1];
var low = bytes[off];
return (high << 8 & 0xFF00) | (low & 0xFF);
},
//byte数组转字符串
byteToString:function(arr) {
if (typeof arr === 'string') {
return arr;
}
var str = '',
_arr = arr;
for (var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if (v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for (var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
return str;
},
//int整数转换为4字节的byte数组
intToByte4:function(i) {
var targets =[];
targets[0] = (i & 0xFF);
targets[1] = (i >> 8 & 0xFF);
targets[2] = (i >> 16 & 0xFF);
targets[3] = (i >> 24 & 0xFF);
return targets;
},
//无符号short转换为2字节的byte数组
unsignedShortToByte2:function(s){
var targets = [];
targets[1] = (s >> 8 & 0xFF);
targets[0] = (s & 0xFF);
return targets;
},
//字符串转byte数组
stringToByte:function(str) {
var bytes = new Array();
var len, c;
len = str.length;
for(var i = 0; i < len; i++) {
c = str.charCodeAt(i);
if(c >= 0x010000 && c <= 0x10FFFF) {
bytes.push(((c >> 18) & 0x07) | 0xF0);
bytes.push(((c >> 12) & 0x3F) | 0x80);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if(c >= 0x000800 && c <= 0x00FFFF) {
bytes.push(((c >> 12) & 0x0F) | 0xE0);
bytes.push(((c >> 6) & 0x3F) | 0x80);
bytes.push((c & 0x3F) | 0x80);
} else if(c >= 0x000080 && c <= 0x0007FF) {
bytes.push(((c >> 6) & 0x1F) | 0xC0);
bytes.push((c & 0x3F) | 0x80);
} else {
bytes.push(c & 0xFF);
}
}
return bytes;
},
//有符int转无符int
int2uint:function(i) {
if (i >= 0)
return i;
else
4294967296 + i;
},
//无符int转有符int
uint2int:function(i) {
if (i <= 2147483647)
return i;
else
return i - 4294967296
},
//有符char转无符char
char2uchar:function(i) {
if (i >= 0)
return i;
else
65535 + i;
},
//无符char转有符char
uchar2char:function(i) {
if (i <= 32767)
return i;
else
return i - 65535
},
//有符byte转无符byte
bytes2ubytes:function(i) {
if (i >= 0)
return i;
else
255 + i;
},
//无符byte转有符byte
ubytes2bytes:function(i) {
if (i <= 127)
return i;
else
return i - 255
}
}

  

最新文章

  1. Apache 配置多端口
  2. LeetCode 【Single Number I II III】
  3. mysql时间日期相加相减实现
  4. AngularJs学习笔记--directive
  5. 在ACCESS中创建数据库和查询(ACCESS 2000)
  6. 结构体 row_prebuilt_t
  7. sae后台管理的js(二)
  8. 设计 REST 风格的 MVC 框架
  9. 【转】无废话WCF系列教程
  10. C++中强制变换之const_cast
  11. ASP.NET获取IP的6种方法(转载于LanceZhang&#39;s Tech Blog)
  12. 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似
  13. escape()、encodeURI()、encodeURIComponent()区别详解(转)
  14. 浅谈 URI 及其转义
  15. [LeetCode] Shopping Offers 购物优惠
  16. STOMP协议规范
  17. 【安富莱TCPnet网络教程】HTTP通信实例
  18. python 配置文件__ConfigParser
  19. hibernate框架中注意的几个问题
  20. postman 介绍

热门文章

  1. PHP ucwords() 函数
  2. luogu P4724 模板 三维凸包
  3. x86架构:实模式下的中断
  4. python 调用百度接口 做人脸识别
  5. tracebace用法
  6. 基于asp.net core 从零搭建自己的业务框架(三)
  7. “随手记”开发记录day10
  8. JavaScript的函数和作用域闭包
  9. ArrayList继承关系分析
  10. Netty之旅:你想要的NIO知识点,这里都有!