How to convert int to byte array and then back?

I need to send an integer through NetworkStream. The problem is that I can only send bytes. This is why I need to divide the integer into four bytes and send them, and on the other hand convert it back to int.

Now I only need this in C #. But for the final project, I will need to convert four bytes to int in Lua.

[EDIT] How about in Lua?

+3
source share
8 answers

Try

BitConverter.GetBytes ()

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

Just keep in mind that the byte order in the returned array depends on the fit of your system.

EDIT: Lua, , . 16, 4. , , -, . , ,

, #?

+6

BitConverter - , , .

int foo = int.MaxValue;
byte lolo = (byte)(foo & 0xff);
byte hilo = (byte)((foo >> 8) & 0xff);
byte lohi = (byte)((foo >> 16) & 0xff);
byte hihi = (byte)(foo >> 24);

.. BitConverter , .

public static unsafe byte[] GetBytes(int value)
{
   byte[] buffer = new byte[4];
   fixed (byte* numRef = buffer)
   {
      *((int*) numRef) = value;
   }
   return buffer;
}
+8

Lua 32- 32- . .

-- convert a 32-bit two complement integer into a four bytes (network order)
function int_to_bytes(n)
  if n > 2147483647 then error(n.." is too large",2) end
  if n < -2147483648 then error(n.." is too small",2) end
  -- adjust for 2 complement
  n = (n < 0) and (4294967296 + n) or n
  return (math.modf(n/16777216))%256, (math.modf(n/65536))%256, (math.modf(n/256))%256, n%256
end

-- convert bytes (network order) to a 32-bit two complement integer
function bytes_to_int(b1, b2, b3, b4)
  if not b4 then error("need four bytes to convert to int",2) end
  local n = b1*16777216 + b2*65536 + b3*256 + b4
  n = (n > 2147483647) and (n - 4294967296) or n
  return n
end

print(int_to_bytes(256)) --> 0 0 1 0
print(int_to_bytes(-10)) --> 255 255 255 246
print(bytes_to_int(255,255,255,246)) --> -10
+2

Lua Roberto struct. ( Lua.) , , , int .

, (, , , ), 4- :

buffer = struct.pack("l", value)

:

value = struct.unpack( "l", buffer)

buffer Lua, . Lua, string.byte - .

, "l" "<l" little-endian ">l" big-endian.

struct C DLL , Lua. , Lua Windows, , Lua Windows.

+2

BinaryWriter/BinaryReader

+1

int : BitConverter...

www.java2s.com/Tutorial/CSharp/0280__Development/Convertaninttoabytearrayanddisplay.htm

- Visual Basic.NET

http://bytes.com/topic/visual-basic-net/answers/349731-integer-byte

int ( #)

http://msdn.microsoft.com/en-us/library/bb384066.aspx

+1

Nubsis, BitConverter , .

EndianBitConverter MiscUtil, . , (int), .

BinaryWriter - , . ( , MiscUtil EndianBinaryWriter, .)

+1

[]:

BitConverter.GetBytes()

http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx

int:

BitConverter.ToInt32 (byteArray, offset)

http://msdn.microsoft.com/en-us/library/system.bitconverter.toint32.aspx

Lua, .

, John Skeet EndianBitConverter. , .

# htons ntohs :

But they only work with signed int16, int32, int64, which means that you will probably end up doing a lot of unnecessary casting to make them work, and if you use the highest order bit for anything other than signing an integer , you are drunk. Was there, did it. :: tsk :: tsk :: Microsoft does not provide the best support for converting endianness to .NET.

+1
source

Source: https://habr.com/ru/post/1751327/


All Articles