1

I have a nodejs buffer:

    var buffer = new Buffer([33,49,0,32,0,0,0,0,2,230,69,56,0,1,125,181,99,99,136,122,92,1,99,196,231,90,205,20,75,233,5,103]);
    var value = buffer.readUInt32BE(8);
    //value == 48645432

I tried read value from C#:

    var buffer = new byte[]{33,49,0,32,0,0,0,0,2,230,69,56,0,1,125,181,99,99,136,122,92,1,99,196,231,90,205,20,75,233,5,103};
    var value = BitConverter.ToUInt32(buffer, 8)
    //value == 944104962

So, I need get value 48645432 from my C# code.

How can I write a method from C# return exact value like readUInt32BE from nodejs?

EDIT: How to get little endian data from big endian in c# using bitConverter.ToInt32 method? not resolve my problem

Minh Giang
  • 631
  • 9
  • 28
  • Possible duplicate of [How to get little endian data from big endian in c# using bitConverter.ToInt32 method?](https://stackoverflow.com/questions/8241060/how-to-get-little-endian-data-from-big-endian-in-c-sharp-using-bitconverter-toin) – bgfvdu3w Oct 01 '17 at 06:10
  • No, please read my question and try some value – Minh Giang Oct 01 '17 at 06:15
  • 1
    `BitConverter.ToUInt32` [reads data as LE](https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readuint32(v=vs.110).aspx#Anchor_2). If you want to pass BE data to it, you'd have to swap endianness first. – bgfvdu3w Oct 01 '17 at 06:17
  • how to swap endianness? please – Minh Giang Oct 01 '17 at 06:21
  • See [this](https://stackoverflow.com/a/19560621/5717792). – bgfvdu3w Oct 01 '17 at 06:42
  • not resolve my problem. – Minh Giang Oct 01 '17 at 06:48
  • 1
    Use `BitConverter.ToUInt32`, then swap endianness. [Working example](https://ideone.com/z53GaW). I linked to the wrong docs in a previous comment. The function assumes the array reflects the endianness of the computer system's architecture. – bgfvdu3w Oct 01 '17 at 06:50

1 Answers1

0

I resolve my problem

    var buffer = new byte[] { 33, 49, 0, 32, 0, 0, 0, 0, 2, 230, 69, 56, 0, 1, 125, 181, 99, 99, 136, 122, 92, 1, 99, 196, 231, 90, 205, 20, 75, 233, 5, 103 };
    var value = BitConverter.ToUInt32(buffer, 8);
    var rs = BitConverter.ToUInt32(BitConverter.GetBytes(value).Reverse().ToArray(), 0);
    //rs = 48645432
Minh Giang
  • 631
  • 9
  • 28