]> cvs.zerfleddert.de Git - proxmark3-svn/blobdiff - client/lualibs/utils.lua
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / client / lualibs / utils.lua
index e84f70ada526e99b36b69f5077f5be8208e2c587..3015c4195c98c55080bb50477a2fc1930a7a9046 100644 (file)
@@ -108,6 +108,24 @@ local Utils =
                return retval\r
        end,\r
        \r
+       -- input parameter is a string\r
+       -- Swaps the endianess and returns a string,  \r
+       -- IE:  'cd7a' -> '7acd'  -> 0x7acd\r
+       SwapEndiannessStr = function(s, len)\r
+               if s == nil then return nil end\r
+               if #s == 0 then return '' end\r
+               if  type(s) ~= 'string' then return nil end\r
+               \r
+               local retval\r
+               if len == 16 then\r
+                       retval = s:sub(3,4)..s:sub(1,2)\r
+               elseif len == 24 then\r
+                       retval = s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+               elseif len == 32 then\r
+                       retval = s:sub(7,8)..s:sub(5,6)..s:sub(3,4)..s:sub(1,2)\r
+               end\r
+               return retval\r
+       end,    \r
        ------------ CONVERSIONS\r
        \r
        --\r
@@ -116,7 +134,7 @@ local Utils =
                local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r
                while IN>0 do\r
                        I=I+1\r
-                       IN,D=math.floor(IN/B),math.mod(IN,B)+1\r
+                       IN , D = math.floor(IN/B), math.modf(IN,B)+1\r
                        OUT=string.sub(K,D,D)..OUT\r
                end\r
                return OUT\r
@@ -129,7 +147,7 @@ local Utils =
                end\r
                local s={}\r
                for i = 1, #(bytes) do\r
-                       s[i] =   string.format("%02X",bytes[i]) \r
+                       s[i] = string.format("%02X",bytes[i]) \r
                end\r
                return table.concat(s)\r
        end,    \r
Impressum, Datenschutz