12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- function inArray(arr, key, val) {
- if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
- return -1
- }
- for (let i = 0; i < arr.length; i++) {
- if (!key) {
- if (arr[i] == val) {
- return i
- }
- } else if (arr[i][key] === val) {
- return i
- }
- }
- return -1;
- }
-
- // ArrayBuffer转16进度字符串示例
- function ab2hex(buffer, split) {
- var hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join(split);
- }
-
- const {TextDecoder, TextEncoder} = require('./encoding')
-
- function hex2str(arr) {
- let decoder = new TextDecoder('utf8')
- let uint8 = new Uint8Array(arr)
- let res = decoder.decode(uint8)
- return res
- // if (!arr.length) {
- // return
- // }
- // let ret = ""
- // arr.forEach(item=>{
- // ret += '%' + item.toString(16)
- // })
- // console.log(ret)
- // return decodeURIComponent(ret)
- }
-
- function str2hex(str) {
- let encoder = new TextEncoder('utf8')
- return encoder.encode(str)
- // let temp = encodeURIComponent(str).split('%').slice(1)
- // let ret = temp.map(item=>{
- // return parseInt(item, 16)
- // })
- // return ret
- }
-
- module.exports = {
- inArray,
- ab2hex,
- hex2str,
- str2hex,
- }
|