JAVASCRIPT 对有符号整型、无符号整型、浮点型、十六进制、二进制的数据处理
1.十六进制字符串转有符号整型,支持S8、S16、S32:
function hexToInt(hex) {
if(hex.length % 2 != 0) {
hex = "0" + hex;
}
var num = parseInt(hex, 16);
var maxVal = Math.pow(2, hex.length / 2 * 8);
if(num > maxVal / 2 - 1) {
num = num - maxVal
}
return num;
}
2.十六进制字符串转无符号整型,支持U8、U16、U32:
parseInt(str,16);
3.十六进制字符串转有/无符号64位整型,支持S64、U64:
function HexStringToInt64StringConverter(signed) {
var hexCode = {
'0': "0000",
'1': "0001",
'2': "0010",
'3': "0011",
'4': "0100",
'5': "0101",
'6': "0110",
'7': "0111",
'8': "1000",
'9': "1001",
'a': "1010",
'b': "1011",
'c': "1100",
'd': "1101",
'e': "1110",
'f': "1111"
};
var preComputedLongMath = {
"20": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
"21": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
"22": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
"23": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8],
"24": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6],
"25": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2],
"26": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 4],
"27": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 8],
"28": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 6],
"29": [0, 0, 0, 0, 0, 0, 0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, 5, 1, 8, 8, 0, 7, 5, 8, 5, 5, 8, 7, 2],
"258": [0, 0, 2, 8, 8, 2, 3, 0, 3, 7, 6, 1, 5, 1, 7, 1, 1, 7, 4, 4],
"259": [0, 0, 5, 7, 6, 4, 6, 0, 7, 5, 2, 3, 0, 3, 4, 2, 3, 4, 8, 8],
"260": [0, 1, 1, 5, 2, 9, 2, 1, 5, 0, 4, 6, 0, 6, 8, 4, 6, 9, 7, 6],
"261": [0, 2, 3, 0, 5, 8, 4, 3, 0, 0, 9, 2, 1, 3, 6, 9, 3, 9, 5, 2],
"262": [0, 4, 6, 1, 1, 6, 8, 6, 0, 1, 8, 4, 2, 7, 3, 8, 7, 9, 0, 4],
"263": [0, 9, 2, 2, 3, 3, 7, 2, 0, 3, 6, 8, 5, 4, 7, 7, 5, 8, 0, 8],
"264": [1, 8, 4, 4, 6, 7, 4, 4, 0, 7, 3, 7, 0, 9, 5, 5, 1, 6, 1, 6],
"265": [3, 6, 8, 9, 3, 4, 8, 8, 1, 4, 7, 4, 1, 9, 1, 0, 3, 2, 3, 2]
};
if(typeof(signed) != 'boolean') signed = false; function toBinary(hex) {
hex = hex.toLowerCase();
var binary = "";
for(var i = 0; i < hex.length; i++) {
binary += hexCode[hex[i]];
}
return binary;
} function to1nsComplement(binary) {
var compliment = "";
for(var i = 0; i < binary.length; i++) {
compliment += (binary.charAt(i) == "1" ? "0" : "1");
}
return compliment;
} function arrayAdd(a, b) {
var carry = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for(var i = 19; i >= 0; i--) {
number[i] = a[i] + b[i] + carry;
if(number[i].toString().length > 1) {
carry = parseInt(number[i].toString().substring(0, number[i].toString().length - 1), 10);
number[i] = parseInt(number[i].toString().substring(number[i].toString().length - 1), 10)
} else {
carry = 0;
}
}
return number;
} function removeZeroPad(number) {
var lock = false;
var output = [];
for(var i = 0; i < number.length; i++) {
if(lock) {
output.push(number[i]);
} else {
if(number[i] != 0) {
lock = true;
output.push(number[i]);
}
}
}
return output;
} function binaryToDec(binary) {
var negative = false;
if(signed && (binary.charAt(0) == 1)) {
negative = true;
}
if(signed) {
binary = binary.substring(1);
if(negative) {
binary = to1nsComplement(binary);
}
}
var pos = 0;
var number = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for(var i = binary.length - 1; i >= 0; i--) {
if(binary.charAt(i) == 1) {
number = arrayAdd(number, preComputedLongMath["2" + pos])
}
pos++;
}
if(negative) {
number = removeZeroPad(arrayAdd(number, preComputedLongMath["20"]));
number.splice(0, 0, "-");
} else {
number = removeZeroPad(number);
}
return number.join("");
} this.convert = function(hex) {
var binary = toBinary(hex);
return binaryToDec(binary);
};
}
使用方法:
lid64 = new HexStringToInt64StringConverter(true).convert(str);
//true为有符号、false为无符号,str为十六进制字符串
4.二进制字符串与十六进制字符串相互转换:
//二进制转十六进制
function bin_to_hex(str) {
let hex_array = [{
key: 0,
val: "0000"
}, {
key: 1,
val: "0001"
}, {
key: 2,
val: "0010"
}, {
key: 3,
val: "0011"
}, {
key: 4,
val: "0100"
}, {
key: 5,
val: "0101"
}, {
key: 6,
val: "0110"
}, {
key: 7,
val: "0111"
},
{
key: 8,
val: "1000"
}, {
key: 9,
val: "1001"
}, {
key: 'a',
val: "1010"
}, {
key: 'b',
val: "1011"
}, {
key: 'c',
val: "1100"
}, {
key: 'd',
val: "1101"
}, {
key: 'e',
val: "1110"
}, {
key: 'f',
val: "1111"
}
]
let value = ''
let list = []
//console.log(str)
if(str.length % 4 !== 0) {
let a = "0000"
let b = a.substring(0, 4 - str.length % 4)
str = b.concat(str)
}
//console.log(str)
while(str.length > 4) {
list.push(str.substring(0, 4))
str = str.substring(4);
}
list.push(str)
//console.log(list)
for(let i = 0; i < list.length; i++) {
for(let j = 0; j < hex_array.length; j++) {
if(list[i] == hex_array[j].val) {
value = value.concat(hex_array[j].key)
break
}
}
}
//console.log(value)
return value } //十六进制转二进制
function hex_to_bin(str) {
let hex_array = [{
key: 0,
val: "0000"
}, {
key: 1,
val: "0001"
}, {
key: 2,
val: "0010"
}, {
key: 3,
val: "0011"
}, {
key: 4,
val: "0100"
}, {
key: 5,
val: "0101"
}, {
key: 6,
val: "0110"
}, {
key: 7,
val: "0111"
},
{
key: 8,
val: "1000"
}, {
key: 9,
val: "1001"
}, {
key: 'a',
val: "1010"
}, {
key: 'b',
val: "1011"
}, {
key: 'c',
val: "1100"
}, {
key: 'd',
val: "1101"
}, {
key: 'e',
val: "1110"
}, {
key: 'f',
val: "1111"
}
] let value = ""
for(let i = 0; i < str.length; i++) {
for(let j = 0; j < hex_array.length; j++) {
if(str.charAt(i) == hex_array[j].key) {
value = value.concat(hex_array[j].val)
break
}
}
}
//console.log(value)
return value
}
5.十六进制字符串与FLOAT互转
function InsertString(t, c, n) {
var r = new Array();
for(var i = 0; i * 2 < t.length; i++) {
r.push(t.substr(i * 2, n));
}
return r.join(c);
} function FillString(t, c, n, b) {
if((t == "") || (c.length != 1) || (n <= t.length)) {
return t;
}
var l = t.length;
for(var i = 0; i < n - l; i++) {
if(b == true) {
t = c + t;
} else {
t += c;
}
}
return t;
} function HexToSingle(t) {
t = t.replace(/\s+/g, "");
if(t == "") {
return "";
}
if(t == "00000000") {
return "0";
}
if((t.length > 8) || (isNaN(parseInt(t, 16)))) {
return "Error";
}
if(t.length < 8) {
t = FillString(t, "0", 8, true);
}
t = parseInt(t, 16).toString(2);
t = FillString(t, "0", 32, true);
var s = t.substring(0, 1);
var e = t.substring(1, 9);
var m = t.substring(9);
e = parseInt(e, 2) - 127;
m = "1" + m;
if(e >= 0) {
m = m.substr(0, e + 1) + "." + m.substring(e + 1)
} else {
m = "0." + FillString(m, "0", m.length - e - 1, true)
}
if(m.indexOf(".") == -1) {
m = m + ".0";
}
var a = m.split(".");
var mi = parseInt(a[0], 2);
var mf = 0;
for(var i = 0; i < a[1].length; i++) {
mf += parseFloat(a[1].charAt(i)) * Math.pow(2, -(i + 1));
}
m = parseInt(mi) + parseFloat(mf);
if(s == 1) {
m = 0 - m;
}
return m;
} function SingleToHex(t) {
if(t == "") {
return "";
}
t = parseFloat(t);
if(isNaN(t) == true) {
return "Error";
}
if(t == 0) {
return "00000000";
}
var s,
e,
m;
if(t > 0) {
s = 0;
} else {
s = 1;
t = 0 - t;
}
m = t.toString(2);
if(m >= 1) {
if(m.indexOf(".") == -1) {
m = m + ".0";
}
e = m.indexOf(".") - 1;
} else {
e = 1 - m.indexOf("1");
}
if(e >= 0) {
m = m.replace(".", "");
} else {
m = m.substring(m.indexOf("1"));
}
if(m.length > 24) {
m = m.substr(0, 24);
} else {
m = FillString(m, "0", 24, false)
}
m = m.substring(1);
e = (e + 127).toString(2);
e = FillString(e, "0", 8, true);
var r = parseInt(s + e + m, 2).toString(16);
r = FillString(r, "0", 8, true);
return InsertString(r, " ", 2).toUpperCase();
} function FormatHex(t, n, ie) {
var r = new Array();
var s = "";
var c = 0;
for(var i = 0; i < t.length; i++) {
if(t.charAt(i) != " ") {
s += t.charAt(i);
c += 1;
if(c == n) {
r.push(s);
s = "";
c = 0;
}
}
if(ie == false) {
if((i == t.length - 1) && (s != "")) {
r.push(s);
}
}
}
return r.join("\n");
} function FormatHexBatch(t, n, ie) {
var a = t.split("\n");
var r = new Array();
for(var i = 0; i < a.length; i++) {
r[i] = FormatHex(a[i], n, ie);
}
return r.join("\n");
} function HexToSingleBatch(t) {
var a = FormatHexBatch(t, 8, true).split("\n");
var r = new Array();
for(var i = 0; i < a.length; i++) {
r[i] = HexToSingle(a[i]);
}
return r.join("\r\n");
} function SingleToHexBatch(t) {
var a = t.split("\n");
var r = new Array();
for(var i = 0; i < a.length; i++) {
r[i] = SingleToHex(a[i]);
}
return r.join("\r\n");
}
使用方法:
//十六进制字符串转浮点
HexToSingle(str)
//浮点转十六进制字符串
SingleToHex(str)
JAVASCRIPT 对有符号整型、无符号整型、浮点型、十六进制、二进制的数据处理的更多相关文章
- C-基础:表达式中存在有符号类型和无符号类型时,都自动转换为无符号类型
void foo(void) { unsigned ; ; (a+b > ) puts("> 6") : puts("<= 6"); } 答案 ...
- c++ 有符号int和无符号int做加减乘除问题
c++ 有符号int和无符号int做加算术运算的问题: 一.运算过程先把有符号的补码数直接看成无符号数,在和无符号数进行算术运算 二.int和unsigned int类型进行混合算数运算时,运算结果为 ...
- Java,C 位移运算符 有符号右移>>与无符号右移>>>
个人博客 地址:https://www.wenhaofan.com/a/20181029232749 有符号右移 正数有符号右移 首先计算4>>2 将4转为二进制 0000 0100 右移 ...
- JavaScript 32位整型无符号操作
在 JavaScript 中,所有整数字变量默认都是有符号整数,这意味着什么呢? 有符号整数使用 31 位表示整数的数值,用第 32 位表示整数的符号,0 表示正数,1 表示负数. 数值范围从 -2^ ...
- javascript 按位或(|),无符号右移(>>>)运算,组合技巧来实现————密码强度提示,四种情况??
直接上代码,原来的代码中,switch中的第一个case,判断之后,少加了个break 跳出判断语句,害得我查了半天,“怎么样式老是不对,不科学啊,呵呵,原来是没跳出case的判断了,还会执行后面的判 ...
- java中符号类型和无符号类型的问题分析
一 参考博文 java中无符号类型的解决方案 二 java中的无符号数和有符号数 在计算机中,可以区分正负的类型,称为有符号类型,无正负的类型,称为无符号类型. 使用二进制中的最高位表示正负 计算机中 ...
- 有符号char转无符号short
; cout<<(int)ch<<endl; //-1 unsigned short d = ch; short dd = ch; cout<<d<<e ...
- Java-java中的有符号,无符号操作以及DataInputStream
1. 无符号和有符号 计算机中用补码表示负数,并且有一定的计算方式:另外,用二进制的最高位表示符号,0表示正数.1表示负数.这种说法本身没错,可是要有一定的解释,不然它就是错的,至少不能解释,为什么字 ...
- Java 与无符号那些事儿
最近在使用 Java 作为 WebSocket 客户端连接 Node.js 的 WebSocket 服务器的时候,由于使用的客户端库比较老,所以遇到了字节符号的问题,上网查了一下,看到这篇文章写的很有 ...
- 获取java byte的无符号数值
byte a = (byte)234; System.out.println(a); 上面的代码,结果是-22,因为java中byte是有符号的,byte范围是-128~127. 如果想输出234,该 ...
随机推荐
- sheet.getLastRowNum()获取行数不准的问题
// 获得总共有多少行int rowNum = 0;//存在样式的空行.会被统计进来.所以主要的问题是要判断是否是空行.for (int num = 1; num <= sheet.getLas ...
- 【Linux】docker学习
docker 使用加速器可以提升获取Docker官方镜像的速度 方法: 安装/升级Docker客户端 推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce 配置镜像加速器 针 ...
- JAVA 【SM2】加密解密
JAVA [SM2]加密解密 前言:最近项目中必须用到SM2的加密解密 引入的Maven依赖 <dependency> <groupId>cn.hutool</group ...
- c语言中定义局部变量不赋初值默认
C语言中定义局部变量不赋初值默认为随机数,全局变量定义时候不赋初值默认为0. 但是在keil3中我发现不管全局变量还是局部变量都默认是0.
- react项目--路由封装
import React, { lazy } from "react"; import Home from "../views/Home"; import Lo ...
- win10:你需要来自XXXX的权限才能对此文件夹进行更改
转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/16769720.html 起因 软件运行失败,看报错信息是删除某个文件夹失败了,行吧,我自己来删.找到目标文 ...
- CCF 201812-1 小明上学
#include <iostream> #include <bits/stdc++.h> #include <string> using namespace std ...
- 虚拟 DOM 的优缺点
优点: 保证性能下限: 框架的虚拟 DOM 需要适配任何上层 API 可能产生的操作,它的一些 DOM 操作的实现必须是普适的,所以它的性能并不是最优的:但是比起粗暴的 DOM 操作性能要好很多,因此 ...
- TP开发项目时遇到的问题记录
1.下载功能. TP自带Http下载类,使用时new一个就行,示例代码: public function download(){ //接收公文id $id = I('get.fid'); //根据公文 ...
- iOS Programing
ARC 1. ARC - Automatic Reference Counting 办公室照明原理 2. 函数release立即释放,autorelease进入autoreleasepool里 3. ...