js将hex字符串转成数字,hexStr2number

116 阅读6分钟

js实现

/**
 * 将hexStr转成数字
 * @param {string} hexStr
 * @param {string} type
 * @param {boolean} littleEndian
 * @returns {number}
 */
function hexStr2number(hexStr, type = "int16", littleEndian = false) {

  function hexStr2bytes(hexStr) {
    let bytes = [];
    for (let i = 0; i < hexStr.length; i += 2)
      bytes.push(parseInt(hexStr.substr(i, 2), 16));
    return bytes;
  }

  function bytes2number(bytes, type, littleEndian) {
    let array = new Uint8Array(bytes);

    let buffer = array.buffer;

    let dataView = new DataView(buffer);

    switch(type)
    {
      case "float32":
        return  dataView.getFloat32(0, littleEndian);
      case "float64":
        return  dataView.getFloat64(0, littleEndian);
      case "int8":
        return  dataView.getInt8(0, littleEndian);
      case "uint8":
        return  dataView.getUint8(0, littleEndian);
      case "int16":
        return  dataView.getInt16(0, littleEndian);
      case "uint16":
        return  dataView.getUint16(0, littleEndian);
      case "int32":
        return  dataView.getInt32(0, littleEndian);
      case "uint32":
        return  dataView.getUint32(0, littleEndian);
      case "bigInt64":
        return  dataView.getBigInt64(0, littleEndian);
      case "bigUint64":
        return  dataView.getBigUint64(0, littleEndian);
      default:
        return NaN;
    }
  }

  let bytes = hexStr2bytes(hexStr);

  return bytes2number(bytes, type, littleEndian);
}




/**
 * 将数字转成hexStr
 * @param {number} num
 * @param {string} type
 * @param {boolean} littleEndian
 * @returns {string}
 */
 function number2hexStr(num, type = "int16", littleEndian = false) {

  function bytes2hexStr(bytes) {
    let hexStr = [];
    for (let i = 0; i < bytes.length; i++) {
      hexStr.push((bytes[i] >>> 4).toString(16));
      hexStr.push((bytes[i] & 0x0F).toString(16));
    }
    return hexStr.join("");
  }

  function number2bytes(num, type, littleEndian) {
    let buffer;
    let dataView;

    switch(type)
    {
      case "float32":
        buffer = new Float32Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setFloat32(0, num, littleEndian);
        break;
      case "float64":
        buffer = new Float64Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setFloat64(0, num, littleEndian);
        break;
      case "int8":
        buffer = new Int8Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setInt8(0, num, littleEndian);
        break;
      case "uint8":
        buffer = new Uint8Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setUint8(0, num, littleEndian);
        break;
      case "int16":
        buffer = new Int16Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setInt16(0, num, littleEndian);
        break;
      case "uint16":
        buffer = new Uint16Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setUint16(0, num, littleEndian);
        break;
      case "int32":
        buffer = new Int32Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setInt32(0, num, littleEndian);
        break;
      case "uint32":
        buffer = new Uint32Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setUint32(0, num, littleEndian);
        break;
      case "bigInt64":
        buffer = new BigInt64Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setBigInt64(0, num, littleEndian);
        break;
      case "bigUint64":
        buffer = new BigUint64Array([num]).buffer;
        dataView = new DataView(buffer);
        dataView.setBigUint64(0, num, littleEndian);
        break;
      default:
        return [];
    }

    return new Uint8Array(buffer);
  }

  let bytes = number2bytes(num, type, littleEndian);

  return bytes2hexStr(bytes);
}

/**
 * 将hexStr转成数字
 * @param {string} hexStr
 * @param {string} type
 * @param {boolean} littleEndian
 * @returns {number}
 */
 function hexStr2number(hexStr, type, littleEndian) {

  function hexStr2bytes(hexStr) {
    var bytes = [];
    for (var i = 0; i < hexStr.length; i += 2)
      bytes.push(parseInt(hexStr.substr(i, 2), 16));
    return bytes;
  }

  function bytes2number(bytes, type, littleEndian) {
    var array = new Uint8Array(bytes);

    var buffer = array.buffer;

    var dataView = new DataView(buffer);

    if (type === "float32")
      return  dataView.getFloat32(0, littleEndian);
    else if (type === "float64")
      return  dataView.getFloat64(0, littleEndian);
    else if (type === "int8")
      return  dataView.getInt8(0, littleEndian);
    else if (type === "uint8")
      return  dataView.getUint8(0, littleEndian);
    else if (type === "int16")
      return  dataView.getInt16(0, littleEndian);
    else if (type === "uint16")
      return  dataView.getUint16(0, littleEndian);
    else if (type === "int32")
      return  dataView.getInt32(0, littleEndian);
    else if (type === "uint32")
      return  dataView.getUint32(0, littleEndian);
    else if (type === "bigInt64")
      return  dataView.getBigInt64(0, littleEndian);
    else if (type === "bigUint64")
      return  dataView.getBigUint64(0, littleEndian);
    else
      return NaN;
  }

  var bytes = hexStr2bytes(hexStr);

  return bytes2number(bytes, type, littleEndian);
}



/**
 * 将数字转成hexStr
 * @param {number} num
 * @param {string} type
 * @param {boolean} littleEndian
 * @returns {string}
 */
 function number2hexStr(num, type, littleEndian) {

  function bytes2hexStr(bytes) {
    var hexStr = [];
    for (var i = 0; i < bytes.length; i++) {
      hexStr.push((bytes[i] >>> 4).toString(16));
      hexStr.push((bytes[i] & 0x0F).toString(16));
    }
    return hexStr.join("");
  }

  function number2bytes(num, type, littleEndian) {
    var buffer;
    var dataView;

    if (type === "float32") {
      buffer = new Float32Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setFloat32(0, num, littleEndian);
    }
    else if (type === "float64") {
      buffer = new Float64Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setFloat64(0, num, littleEndian);
    }
    else if (type === "int8") {
      buffer = new Int8Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setInt8(0, num, littleEndian);
    }
    else if (type === "uint8") {
      buffer = new Uint8Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setUint8(0, num, littleEndian);
    }
    else if (type === "int16") {
      buffer = new Int16Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setInt16(0, num, littleEndian);
    }
    else if (type === "uint16") {
      buffer = new Uint16Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setUint16(0, num, littleEndian);
    }
    else if (type === "int32") {
      buffer = new Int32Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setInt32(0, num, littleEndian);
    }
    else if (type === "uint32") {
      buffer = new Uint32Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setUint32(0, num, littleEndian);
    }
    else if (type === "bigInt64") {
      buffer = new BigInt64Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setBigInt64(0, num, littleEndian);
    }
    else if (type === "bigUint64") {
      buffer = new BigUint64Array([num]).buffer;
      dataView = new DataView(buffer);
      dataView.setBigUint64(0, num, littleEndian);
    }
    else {
      return [];
    }

    return new Uint8Array(dataView.buffer);
  }

  var bytes = number2bytes(num, type, littleEndian);

  return bytes2hexStr(bytes);
}

c语言实现

float

  • float to hexStr

#include <stdio.h>
#include <arpa/inet.h>

union float_bytes {
  float f;
  unsigned int i;
  unsigned char bytes[sizeof(float)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <float>\n", argv[0]);
    return -1;
  }

  union float_bytes fb;

  sscanf(argv[1], "%f", &fb.f);

  printf("sizeof(float): %ld\n", sizeof(float));

  printf("%f: %02hhx%02hhx%02hhx%02hhx\n", fb.f, fb.bytes[0], fb.bytes[1], fb.bytes[2], fb.bytes[3]);

  unsigned int i = htonl(fb.i);
  printf("%f: %08x\n", fb.f, i);

  return 0;
}
  • hexStr to float

#include <stdio.h>
#include <arpa/inet.h>

union float_bytes {
  float f;
  unsigned int i;
  unsigned char bytes[sizeof(float)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <float hexStr>\n", argv[0]);
    return -1;
  }

  union float_bytes fb;

  sscanf(argv[1], "%x", &fb.i);

  fb.i = htonl(fb.i);

  printf("%f: %02hhx%02hhx%02hhx%02hhx\n", fb.f, fb.bytes[0], fb.bytes[1], fb.bytes[2], fb.bytes[3]);

  return 0;
}

double

  • double to hexStr

#include <stdio.h>


union double_bytes {
  double d;
  unsigned char bytes[sizeof(double)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <double>\n", argv[0]);
    return -1;
  }

  union double_bytes db;

  sscanf(argv[1], "%lf", &db.d);

  printf("sizeof(doule): %ld\n", sizeof(double));

  printf("%lf: %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx\n", db.d,
      db.bytes[0], db.bytes[1], db.bytes[2], db.bytes[3],
      db.bytes[4], db.bytes[5], db.bytes[6], db.bytes[7]);

  return 0;
}
  • hexStr to double

#include <stdio.h>
#include <arpa/inet.h>

union double_bytes {
  double d;
  unsigned long l;
  unsigned int i[sizeof(double) / sizeof(int)];
  unsigned char bytes[sizeof(double)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <double hexStr>\n", argv[0]);
    return -1;
  }

  union double_bytes db;

  sscanf(argv[1], "%lx", &db.l);

  unsigned int t;
  t = db.i[0];
  db.i[0] = db.i[1];
  db.i[1] = t;

  db.i[0] = htonl(db.i[0]);
  db.i[1] = htonl(db.i[1]);


  printf("sizeof(doule): %ld\n", sizeof(double));

  printf("%lf: %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx\n", db.d,
      db.bytes[0], db.bytes[1], db.bytes[2], db.bytes[3],
      db.bytes[4], db.bytes[5], db.bytes[6], db.bytes[7]);

  return 0;
}

char

  • char to hexStr

#include <stdio.h>


int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <char>\n", argv[0]);
    return -1;
  }

  unsigned char ch;

  sscanf(argv[1], "%hhu", &ch);

  printf("sizeof(char): %ld\n", sizeof(char));

  printf("%hhu: %02hhx\n", ch, ch);

  return 0;
}
  • hexStr to char

#include <stdio.h>


int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <char hexStr>\n", argv[0]);
    return -1;
  }

  unsigned char ch;

  sscanf(argv[1], "%hhx", &ch);

  printf("sizeof(char): %ld\n", sizeof(char));

  printf("%hhu: %02hhx\n", ch, ch);

  return 0;
}

short

  • short to hexStr

#include <stdio.h>


union short_bytes {
  unsigned short num;
  unsigned char bytes[sizeof(unsigned short)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <short>\n", argv[0]);
    return -1;
  }

  union short_bytes sb;

  sscanf(argv[1], "%hu", &sb.num);

  printf("sizeof(short): %ld\n", sizeof(short));

  printf("%hu: %2hhx%02hhx\n", sb.num, sb.bytes[0], sb.bytes[1]);

  return 0;
}
  • hexStr to short

#include <stdio.h>
#include <arpa/inet.h>


union short_bytes {
  unsigned short num;
  unsigned char bytes[sizeof(unsigned short)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <short hexStr>\n", argv[0]);
    return -1;
  }

  union short_bytes sb;

  sscanf(argv[1], "%hx", &sb.num);

  sb.num = htons(sb.num);

  printf("sizeof(short): %ld\n", sizeof(short));

  printf("%hu: %02hhx%02hhx\n", sb.num, sb.bytes[0], sb.bytes[1]);

  return 0;
}

int

  • int to hexStr

#include <stdio.h>


union int_bytes {
  unsigned int num;
  unsigned char bytes[sizeof(unsigned int)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <int>\n", argv[0]);
    return -1;
  }

  union int_bytes ib;

  sscanf(argv[1], "%u", &ib.num);

  printf("sizeof(int): %ld\n", sizeof(int));

  printf("%u: %2hhx%02hhx%02hhx%02hhx\n", ib.num, ib.bytes[0], ib.bytes[1], ib.bytes[2], ib.bytes[3]);

  return 0;
}
  • hexStr to int

#include <stdio.h>
#include <arpa/inet.h>


union int_bytes {
  unsigned int num;
  unsigned char bytes[sizeof(unsigned int)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <int hexStr>\n", argv[0]);
    return -1;
  }

  union int_bytes ib;

  sscanf(argv[1], "%x", &ib.num);

  ib.num = htonl(ib.num);

  printf("sizeof(int): %ld\n", sizeof(int));

  printf("%u: %02hhx%02hhx%02hhx%02hhx\n", ib.num, ib.bytes[0], ib.bytes[1], ib.bytes[2], ib.bytes[3]);

  return 0;
}

long

  • long to hexStr

#include <stdio.h>


union long_bytes {
  unsigned long num;
  unsigned char bytes[sizeof(unsigned long)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <long>\n", argv[0]);
    return -1;
  }

  union long_bytes lb;

  sscanf(argv[1], "%lu", &lb.num);

  printf("sizeof(long): %ld\n", sizeof(long));

  printf("%lu: %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx\n", lb.num,
      lb.bytes[0], lb.bytes[1], lb.bytes[2], lb.bytes[3],
      lb.bytes[4], lb.bytes[5], lb.bytes[6], lb.bytes[7]);

  return 0;
}
  • hexStr to long

#include <stdio.h>
#include <arpa/inet.h>

union long_bytes {
  unsigned long num;
  unsigned int i[sizeof(unsigned long) / sizeof(unsigned int)];
  unsigned char bytes[sizeof(unsigned long)];
};

int main(int argc, char **argv)
{

  if (argc != 2)
  {
    fprintf(stderr, "Usage: %s <long hexStr>\n", argv[0]);
    return -1;
  }

  union long_bytes lb;

  sscanf(argv[1], "%lx", &lb.num);

  unsigned int t;
  t = lb.i[0];
  lb.i[0] = lb.i[1];
  lb.i[1] = t;

  lb.i[0] = htonl(lb.i[0]);
  lb.i[1] = htonl(lb.i[1]);

  printf("sizeof(long): %ld\n", sizeof(long));

  printf("%lu: %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx\n", lb.num,
      lb.bytes[0], lb.bytes[1], lb.bytes[2], lb.bytes[3],
      lb.bytes[4], lb.bytes[5], lb.bytes[6], lb.bytes[7]);

  return 0;
}