0

I am reading a binary in javascript. I think this file was written in C# and the the way binaries handle the strings in C# is a little different than how it is handled in as mentioned in

https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readstring?view=net-7.0

The thing is that I am not able to find any library which allows me to read C# style strings OR 7bitEncodedInt. Any suggestions on any code/package that allows me to do that?

stuartd
  • 70,509
  • 14
  • 132
  • 163
Lost
  • 12,007
  • 32
  • 121
  • 193
  • Take a look at https://stackoverflow.com/questions/43920286/node-js-read-string-with-7-bit-encoded-int-length – Trentium Oct 27 '22 at 01:04
  • I am not very sure why there are votes to close this question? I would like to know if there is a library or a guidance on how to read these values which is documented anywhere? Is it safe to assume that if no one will come across this problem? And when they come across this question, would it be safe to assume that every one is very well-versed in their understanding of byte shifting? I am all for closing it but before we do that, I would like to know the reasoning. I posted the answer below after trying different things for 3 days. I personally feel that this can be helpful to someone else. – Lost Oct 29 '22 at 14:36
  • None of the close votes were mine, but suspect that a lack of response to my comment linking to a Q&A with an algorithm which I believe answers the question was probably the driver for the close votes... – Trentium Oct 29 '22 at 15:46
  • @Lost My close vote was because you were asking for a library, not for understanding how to write a function that does this – Bergi Oct 29 '22 at 15:53

1 Answers1

0

Actually, I found an answer in one of the libraries and modified according to what a getString would look like:

private GetString(buffer) {
    try {
      let length = this.Read7BitEncodedInt(buffer);
      if (length < 0) {
        this.toastrService.error('Error');
      }
      if (length == 0) {
        return '';
      } else {
        return buffer.getNextString(length);
      }
    }
    catch (exception){
      this.toastrService.error('Error')
    }


  }

  private Read7BitEncodedInt(buffer) {
    let count = 0, shift = 0, b = 0, i = 0;
    try {
      do {
        if (shift === 5 * 7) {
          this.toastrService.error('Error');
        }
        //Get a single byte
        b = buffer.getNextBytes(1);
        // tslint:disable-next-line:no-bitwise

        //Update the value of the int based on the byte that it belongs to(shift value)
        count |= (b & 0x7F) << shift;
        shift += 7;
        i++;
        // tslint:disable-next-line:triple-equals no-bitwise
        //Exit if byte is empty
      } while ((b & 0x80) != 0);
    }
    catch (exception){
      this.toastrService.error('Error message');
    }
Lost
  • 12,007
  • 32
  • 121
  • 193
  • 1
    "*I found an answer in one of the libraries*" - then please link your source! – Bergi Oct 29 '22 at 15:51
  • 1
    What is the type of `buffer`? I don't know any Buffer class in JS that has a `getNextBytes` or `getNextString` method – Bergi Oct 29 '22 at 15:54