aboutsummaryrefslogtreecommitdiff
path: root/src/encode/baseN.c
diff options
context:
space:
mode:
authorame <[email protected]>2023-12-25 15:10:09 -0600
committerame <[email protected]>2023-12-25 15:10:09 -0600
commitddb25e94961f0eb9f5f615db83b316d3c222da81 (patch)
treec9cee3eafcf00053952e7965bf47feaf828dfd30 /src/encode/baseN.c
parent086f2b9ee90c72d73513b5b0e2488aa8999b2b00 (diff)
arbitrary base convertions
Diffstat (limited to 'src/encode/baseN.c')
-rw-r--r--src/encode/baseN.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/encode/baseN.c b/src/encode/baseN.c
new file mode 100644
index 0000000..9c911c2
--- /dev/null
+++ b/src/encode/baseN.c
@@ -0,0 +1,53 @@
+/*
+local function to_base10(inp,basein)
+ local out = 0
+ for i=1,#inp do
+ out = out + (inp[i]*(basein^(#inp - i)))
+ end
+ return out
+end
+
+local function from_base10(inp,outbase)
+ local out = {}
+ while inp > 0 do
+ local rem = inp % outbase
+ inp = math.floor(inp / outbase)
+ table.insert(out,1,rem)
+ end
+ return out
+end
+local function baseT_to_N(inp,basein,baseout)
+ return from_base10(to_base10(inp,basein),baseout)
+end
+*/
+#include <math.h>
+#include <stdint.h>
+#include "../lua.h"
+#include "../table.h"
+
+int l_baseconvert(lua_State* L){
+ luaL_checktype(L, 1, LUA_TTABLE);
+ int64_t base_import = luaL_checkinteger(L, 2);
+ int64_t base_export = luaL_checkinteger(L, 3);
+ size_t len = lua_objlen(L,1);
+ uint64_t out = 0;
+
+ for(size_t i = 1; i <= len; i++){
+ lua_pushnumber(L, i);
+ lua_gettable(L, 1);
+
+ out += luaL_checkinteger(L, -1) * pow(base_import, len - i);
+ }
+
+ lua_newtable(L);
+ for(int i = 1; out > 0; i++){
+ uint64_t rem = out % base_export;
+ out /= base_export;
+
+ lua_pushnumber(L,i);
+ lua_pushnumber(L, rem);
+ lua_settable(L, -3);
+ }
+
+ return 1;
+};