how to extract the 4 bytes of a 32bit int in lua

Max L.

I have a lua function that converts ip addresses to 32 bit int

local str = "127.0.0.1"
local o1,o2,o3,o4 = str:match("(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)" )
local num = 2^24*o1 + 2^16*o2 + 2^8*o3 + o4

I would like to have the inverse function, i.e. get the 4 bytes from the int

Paul Kulchenko

You can use bit or bit32 libraries (included in Lua 5.2+ and LuaJIT and available as modules for 5.1). You can also use the reverse operations to what you already have:

print(math.floor(num / 2^24), math.floor((num % 2^24) / 2^16),
  math.floor((num % 2^16) / 2^8), num % 2^8)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Translating SSE to Neon: How to pack and then extract 32bit result

From Dev

Why is std::size_t 4 bytes on 32bit systems when unsigned long long is 8 bytes on both 32bit and 64 bit systems?

From Dev

Building 32bit Float from 4 Bytes [Binary File I/O] in C++

From Dev

Representing Int32 as 4 Bytes in Scala

From Dev

How to interpret 4 bytes as a 32-bit float using Python

From Dev

C# How to Remove leading 0's from 32bit int

From Dev

How can I extract blocks of Bytes from a int32_t and store it in a int16_t or int8_t using c?

From Dev

How to determine if a word(4 bytes) is a 16-bit instruction or 32-bit instruction?

From Dev

extract 32bit from 64bit integer

From Dev

Java: 4 bytes to 32 bit integer

From Dev

Java: 4 bytes to 32 bit integer

From Dev

Ruby: 4-bytes array to int32

From Dev

How do I convert 4 bytes of a ByteString to an Int?

From Dev

Converting 4 bytes to unsigned int

From Dev

How to export Image list of 32bit icons into single 32bit bitmap file?

From Dev

C# change the first 32bit Int of a GUID

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

From Dev

Eclipse - How to know if 32bit or 64bit installation

From Dev

How to reference 32bit integer data in a 64 bit dump

From Dev

Wix: How to write to 32bit registers on 64 bit machine

From Dev

How can I convert a pair of bytes into a signed 16-bit integer using Lua?

From Dev

Converting 4 raw bytes into 32-bit floating point

From Dev

How to combine 4 uint32_t ints into a whole 128 bit int and return

From Dev

Unpack 4 bytes out of int in GLSL

From Dev

sizeof(int) is 4 bytes but only two are written

From Dev

sizeof(int) is 4 bytes but only two are written

From Dev

How to install Docker on 32bit machine having Ubuntu 12.04?

From Dev

How to unpack 32bit integer packed in a QByteArray?

Related Related

  1. 1

    Translating SSE to Neon: How to pack and then extract 32bit result

  2. 2

    Why is std::size_t 4 bytes on 32bit systems when unsigned long long is 8 bytes on both 32bit and 64 bit systems?

  3. 3

    Building 32bit Float from 4 Bytes [Binary File I/O] in C++

  4. 4

    Representing Int32 as 4 Bytes in Scala

  5. 5

    How to interpret 4 bytes as a 32-bit float using Python

  6. 6

    C# How to Remove leading 0's from 32bit int

  7. 7

    How can I extract blocks of Bytes from a int32_t and store it in a int16_t or int8_t using c?

  8. 8

    How to determine if a word(4 bytes) is a 16-bit instruction or 32-bit instruction?

  9. 9

    extract 32bit from 64bit integer

  10. 10

    Java: 4 bytes to 32 bit integer

  11. 11

    Java: 4 bytes to 32 bit integer

  12. 12

    Ruby: 4-bytes array to int32

  13. 13

    How do I convert 4 bytes of a ByteString to an Int?

  14. 14

    Converting 4 bytes to unsigned int

  15. 15

    How to export Image list of 32bit icons into single 32bit bitmap file?

  16. 16

    C# change the first 32bit Int of a GUID

  17. 17

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  18. 18

    Will an int be 32bit and a long 64bit regardless of whether the system is 32 or 64 bit?

  19. 19

    Eclipse - How to know if 32bit or 64bit installation

  20. 20

    How to reference 32bit integer data in a 64 bit dump

  21. 21

    Wix: How to write to 32bit registers on 64 bit machine

  22. 22

    How can I convert a pair of bytes into a signed 16-bit integer using Lua?

  23. 23

    Converting 4 raw bytes into 32-bit floating point

  24. 24

    How to combine 4 uint32_t ints into a whole 128 bit int and return

  25. 25

    Unpack 4 bytes out of int in GLSL

  26. 26

    sizeof(int) is 4 bytes but only two are written

  27. 27

    sizeof(int) is 4 bytes but only two are written

  28. 28

    How to install Docker on 32bit machine having Ubuntu 12.04?

  29. 29

    How to unpack 32bit integer packed in a QByteArray?

HotTag

Archive