Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

344
Views
Función de traducción de GO a Javascript: el rango que consta de desplazamiento y longitud está fuera de los límites

Estoy tratando de traducir una función de GO a javascript (apenas tengo conocimiento de GO).

Esta es la funcion original

 func prepareMessage(data []byte) []byte { // Compute CRC before modifying the message. crc := crcCompute(data) // Add the two CRC16 bytes before replacing control characters. data = append(data, byte(crc&0xFF)) data = append(data, byte(crc>>8)) tmp := []byte{0x7E} // Flag start. // Copy the message over, escaping 0x7E (Flag Byte) and 0x7D (Control-Escape). for i := 0; i < len(data); i++ { mv := data[i] if (mv == 0x7E) || (mv == 0x7D) { mv = mv ^ 0x20 tmp = append(tmp, 0x7D) } tmp = append(tmp, mv) } tmp = append(tmp, 0x7E) // Flag end. return tmp }

Este es mi intento en JavaScript

 var _appendBuffer = function (buffer1, buffer2) { var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength); tmp.set(new Uint8Array(buffer1), 0); tmp.set(new Uint8Array(buffer2), buffer1.byteLength); return tmp.buffer; }; function prepareMessage(data) { // Compute CRC before modifying the message. var crc = crcCompute(data) // Add the two CRC16 bytes before replacing control characters. data = data + (crc & 0xFF); data = data + (crc >> 8); var tmp = new ArrayBuffer(1000000000); tmp[0] = [0x7E]; // Flag start. // Copy the message over, escaping 0x7E (Flag Byte) and 0x7D (Control-Escape). for (var i = 0; i < data.length; i++) { var mv = data[i] if ((mv == 0x7E) || (mv == 0x7D)) { mv = mv ^ 0x20; tmp = _appendBuffer(tmp, [0x7D]); } tmp = _appendBuffer(tmp, mv); } tmp = _appendBuffer(tmp, [0x7E]); // Flag end. return tmp; }

Obtener este error en la función prepareMessage RangeError: Range consisting of offset and length are of bounds en la línea tmp = _appendBuffer(tmp, mv);

Entiendo que algo está mal con los tamaños de búfer, pero no sé cómo solucionarlo.

gracias leon

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

tmp := []byte{0x7E} // Flag start. se declara e inicializa como una matriz con solo 1 elemento en ella. Ya está lleno, prueba a hacer un trozo con más capacidad con la función make() .

Ese es un buen artículo con más detalles. https://www.godesignpatterns.com/2014/05/arrays-vs-slices.html

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!