JavaScript 16进制字符串拼接问题 10C

JavaScript 16进制字符串拼接问题
给的指令是
var a = 'aa 55 00 09 01 11 10 ff f2 81 b0 0d 0a';
通过转译成
var a = '\xaa\x55\x00\x09\x01\x11\x10\xff\xf2\x81\xb0\x0d\x0a';

发送时没问题的

现在就是 需要对指令中的 某一位 通过程序的形式进行更改,比如 \x09 这一把

我写成
var b = 5;
var str = '\x' + b.toString(16);
var a1 = '\xaa\x55\x00'+ str +'\x01\x11\x10\xff\xf2\x81\xb0\x0d\x0a';

这样写是不行的

想问下 js 要怎么拼接16进制的字符串

3个回答

var str = '\x' + b.toString(16);
改为

var str = '\\x' + b.toString(16);
//十六进制字符串转字节数组

function Str2Bytes(str)

{

    var pos = 0;

    var len = str.length;

    if(len %2 != 0)

    {

       return null; 

    }

    len /= 2;

    var hexA = new Array();

    for(var i=0; i<len; i++)

    {

       var s = str.substr(pos, 2);

       var v = parseInt(s, 16);

       hexA.push(v);

       pos += 2;

    }

    return hexA;

}



//字节数组转十六进制字符串

function Bytes2Str(arr)

{

    var str = "";

    for(var i=0; i<arr.length; i++)

    {

       var tmp = arr[i].toString(16);

       if(tmp.length == 1)

       {

           tmp = "0" + tmp;

       }

       str += tmp;

    }

    return str;

}

我找到的解决办法


var b = getHex(calculate(a));
var zl = '\xaa\x55\x00\x09\x01'+ b +'\x11\xff\xf1\x0D\x0A';
// 16进制数据对照表
function getHex(e){
 var  toArray = ['\x00','\x01','\x02','\x03','\x04','\x05','\x06','\x07','\x08','\x09','\x0a','\x0b','\x0c','\x0d','\x0e','\x0f',
                 '\x10','\x11','\x12','\x13','\x14','\x15','\x16','\x17','\x18','\x19','\x1a','\x1b','\x1c','\x1d','\x1e','\x1f',
                 '\x20','\x21','\x22','\x23','\x24','\x25','\x26','\x27','\x28','\x29','\x2a','\x2b','\x2c','\x2d','\x2e','\x2f',
                 '\x30','\x31','\x32','\x33','\x34','\x35','\x36','\x37','\x38','\x39','\x3a','\x3b','\x3c','\x3d','\x3e','\x3f',
                 '\x40','\x41','\x42','\x43','\x44','\x45','\x46','\x47','\x48','\x49','\x4a','\x4b','\x4c','\x4d','\x4e','\x4f',
                 '\x50','\x51','\x52','\x53','\x54','\x55','\x56','\x57','\x58','\x59','\x5a','\x5b','\x5c','\x5d','\x5e','\x5f',
                 '\x60','\x61','\x62','\x63','\x64','\x65','\x66','\x67','\x68','\x69','\x6a','\x6b','\x6c','\x6d','\x6e','\x6f',
                 '\x70','\x71','\x72','\x73','\x74','\x75','\x76','\x77','\x78','\x79','\x7a','\x7b','\x7c','\x7d','\x7e','\x7f',
                 '\x80','\x81','\x82','\x83','\x84','\x85','\x86','\x87','\x88','\x89','\x8a','\x8b','\x8c','\x8d','\x8e','\x8f',
                 '\x90','\x91','\x92','\x93','\x94','\x95','\x96','\x97','\x98','\x99','\x9a','\x9b','\x9c','\x9d','\x9e','\x9f',
                 '\xa0','\xa1','\xa2','\xa3','\xa4','\xa5','\xa6','\xa7','\xa8','\xa9','\xaa','\xab','\xac','\xad','\xae','\xaf',
                 '\xb0','\xb1','\xb2','\xb3','\xb4','\xb5','\xb6','\xb7','\xb8','\xb9','\xba','\xbb','\xbc','\xbd','\xbe','\xbf',
                 '\xc0','\xc1','\xc2','\xc3','\xc4','\xc5','\xc6','\xc7','\xc8','\xc9','\xca','\xcb','\xcc','\xcd','\xce','\xcf',
                 '\xd0','\xd1','\xd2','\xd3','\xd4','\xd5','\xd6','\xd7','\xd8','\xd9','\xda','\xdb','\xdc','\xdd','\xde','\xdf',
                 '\xe0','\xe1','\xe2','\xe3','\xe4','\xe5','\xe6','\xe7','\xe8','\xe9','\xea','\xeb','\xec','\xed','\xee','\xef',
                 '\xf0','\xf1','\xf2','\xf3','\xf4','\xf5','\xf6','\xf7','\xf8','\xf9','\xfa','\xfb','\xfc','\xfd','\xfe','\xff'];
      return toArray[e]

}
// 根据16进制字符串判断在对照表中的下标
function calculate(e){
    var e1 = e[0];
    var e2 = e[1];
    var num = '';
    var num1 = '';
    if(e1 == 'a' || e1 == 'A'){
      e1 = 10;
    }
    if(e1 == 'b' || e1 == 'B'){
      e1 = 11;
    }
    if(e1 == 'c' || e1 == 'C'){
      e1 = 12;
    }
    if(e1 == 'd' || e1 == 'D'){
      e1 = 13;
    }
    if(e1 == 'e' || e1 == 'E'){
      e1 = 14;
    }
    if(e1 == 'f' || e1 == 'F'){
      e1 = 15;
    }
    e1 = parseInt(e1);


    if(e2 == 'a' || e2 == 'A'){
      e2 = 10;
    }
    if(e2 == 'b' || e2 == 'B'){
      e2 = 11;
    }
    if(e2 == 'c' || e2 == 'C'){
      e2 = 12;
    }
    if(e2 == 'd' || e2 == 'D'){
      e2 = 13;
    }
    if(e2 == 'e' || e2 == 'E'){
      e2 = 14;
    }
    if(e2 == 'f' || e2 == 'F'){
      e2 = 15;
    }
    e2 = parseInt(e2);

    switch (e1) {
      case 0:
        num = 0;
        break;
      case 1 :
        num = 16;
        break;
      case 2:
        num = 32;
        break;
      case 3:
        num = 48;
        break;
      case 4:
        num = 64;
        break;
      case 5:
        num = 80;
        break;
      case 6:
        num = 96;
        break;
      case 7:
        num = 112;
        break;
      case 8:
        num = 128;
        break;
      case 9:
        num = 144;
        break;
      case 10:
        num = 160;
        break;
      case 11:
        num = 176;
        break;
      case 12:
        num = 192;
        break;
      case 13:
        num = 208;
        break;
      case 14:
        num = 224;
        break;
      case 15:
        num = 240;
  }

    switch (e2) {
      case 0 :
        num1 = 0;
        break;
      case 1 :
        num1 = 1;
        break;
      case 2:
        num1 = 2;
        break;
      case 3:
        num1 = 3;
        break;
      case 4:
        num1 = 4;
        break;
      case 5:
        num1 = 5;
        break;
      case 6:
        num1 = 6;
        break;
      case 7:
        num1 = 7;
        break;
      case 8:
        num1 = 8;
        break;
      case 9:
        num1 = 9;
        break;
      case 10:
        num1 = 10;
        break;
      case 11:
        num1 = 11;
        break;
      case 12:
        num1 = 12;
        break;
      case 13:
        num1 = 13;
        break;
      case 14:
        num1 = 14;
        break;
      case 15:
        num1 = 15;
  }
  return num + num1;

}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!