util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const gbk = require('./gbk.js');
  2. console.log("sasas" + gbk);
  3. const formatTime = date => {
  4. const year = date.getFullYear()
  5. const month = date.getMonth() + 1
  6. const day = date.getDate()
  7. const hour = date.getHours()
  8. const minute = date.getMinutes()
  9. const second = date.getSeconds()
  10. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  11. }
  12. const formatNumber = n => {
  13. n = n.toString()
  14. return n[1] ? n : '0' + n
  15. }
  16. const hexStringToBuff = str => { //str='中国:WXHSH'
  17. const buffer = new ArrayBuffer((sumStrLength(str)) * 4)
  18. const dataView = new DataView(buffer)
  19. var data = str.toString();
  20. var p = 0; //ArrayBuffer 偏移量
  21. for (var i = 0; i < data.length; i++) {
  22. if (isCN(data[i])) { //是中文
  23. //调用GBK 转码
  24. var t = gbk.encode(data[i]);
  25. for (var j = 0; j < 2; j++) {
  26. //var code = t[j * 2] + t[j * 2 + 1];
  27. var code = t[j * 3 + 1] + t[j * 3 + 2];
  28. var temp = parseInt(code, 16)
  29. //var temp = strToHexCharCode(code);
  30. dataView.setUint8(p++, temp)
  31. }
  32. } else {
  33. var temp = data.charCodeAt(i);
  34. dataView.setUint8(p++, temp)
  35. }
  36. }
  37. return buffer;
  38. }
  39. function toUnicode(s) {
  40. var str = "";
  41. for (var i = 0; i < s.length; i++) {
  42. str += "\\u" + s.charCodeAt(i).toString(16) + "\t";
  43. }
  44. return str;
  45. }
  46. function strToHexCharCode(str) {
  47. if (str === "")
  48. return "";
  49. var hexCharCode = [];
  50. hexCharCode.push("0x");
  51. for (var i = 0; i < str.length; i++) {
  52. hexCharCode.push((str.charCodeAt(i)).toString(16));
  53. }
  54. return hexCharCode.join("");
  55. }
  56. function sumStrLength(str) {
  57. var length = 0;
  58. var data = str.toString();
  59. for (var i = 0; i < data.length; i++) {
  60. if (isCN(data[i])) { //是中文
  61. length += 2;
  62. } else {
  63. length += 1;
  64. }
  65. }
  66. return length;
  67. }
  68. function isCN(str) {
  69. if (/^[\u3220-\uFA29]+$/.test(str)) {
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. //汉字转码
  76. function hexStringToArrayBuffer(str) {
  77. const buffer = new ArrayBuffer((str.length / 2) + 1)
  78. const dataView = new DataView(buffer)
  79. for (var i = 0; i < str.length / 2; i++) {
  80. var temp = parseInt(str[i * 2] + str[i * 2 + 1], 16)
  81. dataView.setUint8(i, temp)
  82. }
  83. dataView.setUint8((str.length / 2), 0x0a)
  84. return buffer;
  85. }
  86. //返回八位数组
  87. function subString(str) {
  88. var arr = [];
  89. if (str.length > 8) { //大于8
  90. for (var i = 0;
  91. (i * 8) < str.length; i++) {
  92. var temp = str.substring(i * 8, 8 * i + 8);
  93. arr.push(temp)
  94. }
  95. return arr;
  96. } else {
  97. return str
  98. }
  99. }
  100. //不带有汉字
  101. function hexStringToArrayBufferstr(str) {
  102. let val = ""
  103. for (let i = 0; i < str.length; i++) {
  104. if (val === '') {
  105. val = str.charCodeAt(i).toString(16)
  106. } else {
  107. val += ',' + str.charCodeAt(i).toString(16)
  108. }
  109. }
  110. val += "," + "0x0a";
  111. console.log(val)
  112. // 将16进制转化为ArrayBuffer
  113. return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function(h) {
  114. return parseInt(h, 16)
  115. })).buffer
  116. }
  117. function send0X0A() {
  118. const buffer = new ArrayBuffer(1)
  119. const dataView = new DataView(buffer)
  120. dataView.setUint8(0, 0x0a)
  121. return buffer;
  122. }
  123. module.exports = {
  124. hexStringToArrayBuffer: hexStringToArrayBuffer,
  125. hexStringToBuff: hexStringToBuff,
  126. send0X0A: send0X0A
  127. }