Convert a binary string to big endian integer in the browser?

I have the following string in binary : const bin = ‘\x00\x00\x16%’ I’d like to convert it in big endian integer. I was able to do it using the following: new DataView(Uint8Array.from(‘\x00\x00\x16%’, c => c.charCodeAt(0)).buffer).getInt32(0, false) => 5669 But I’m pretty sure there is asimpler way to do so, rather than convert to an Uint8Array… Read More Convert a binary string to big endian integer in the browser?

Using htons() in my code puts all zeros in the buffer and I don't understand why

I need to use htons() in my code to convert the little-endian ordered short to a network (big-endian) byte order. I have this code: int PacketInHandshake::serialize(SOCKET connectSocket, BYTE* outBuffer, ULONG outBufferLength) { memset(outBuffer, 0, outBufferLength); const int sizeOfShort = sizeof(u_short); u_short userNameLength = (u_short)strlen(userName); u_short osVersionLength = (u_short)strlen(osVersion); int dataLength = 1 + (sizeOfShort *… Read More Using htons() in my code puts all zeros in the buffer and I don't understand why