From 37c5ae6f58cc7e14aa7f4461c37f934e0b1933b2 Mon Sep 17 00:00:00 2001 From: hanez Date: Thu, 18 Dec 2025 10:29:50 +0100 Subject: [PATCH] Added some more HEX (dec_to_hex()) to Fun. (0.37.15) --- CMakeLists.txt | 2 +- examples/hex_example.fun | 12 +++++++++++- examples/test_dec_to_hex.fun | 23 +++++++++++++++++++++++ examples/test_include.fun | 19 +++++++++++++++++++ lib/hex.fun | 12 ++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 examples/test_dec_to_hex.fun create mode 100644 examples/test_include.fun diff --git a/CMakeLists.txt b/CMakeLists.txt index d33e6ae..743767a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.37.14 LANGUAGES C) +project(fun VERSION 0.37.15 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/examples/hex_example.fun b/examples/hex_example.fun index 2b6074e..84865e2 100755 --- a/examples/hex_example.fun +++ b/examples/hex_example.fun @@ -32,7 +32,7 @@ print("Hex bytes array:") bytes = hex_to_bytes(hexstr) print(to_string(bytes)) -echo("Hex dump to bytes:") +print("Hex dump to bytes:") print(bytes) print("Back to hex from bytes:") @@ -44,6 +44,12 @@ print(to_string(hex_to_dec(substr(hexstr, 0, 8)))) print("Hex as decimal (all bytes):") print(to_string(hex_to_dec(hexstr))) +print("Decimal to hex (4135093009263527588):") +print(dec_to_hex(4135093009263527588)) + +print("Decimal to hex (1353150228):") +print(dec_to_hex(1353150228)) + /* Expected output: --- Hex utility demo (fixed values) --- Fixed Hex string: @@ -59,4 +65,8 @@ Hex as decimal (first 4 bytes - 0x50a76f14): 1353150228 Hex as decimal (all bytes): 4135093009263527588 +Decimal to hex (4135093009263527588): +3962cd2eea5a86a4 +Decimal to hex (1353150228): +50a76f14 */ diff --git a/examples/test_dec_to_hex.fun b/examples/test_dec_to_hex.fun new file mode 100644 index 0000000..05a2f69 --- /dev/null +++ b/examples/test_dec_to_hex.fun @@ -0,0 +1,23 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2025 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2025-12-18 + */ + +#include + +hexstr = "ff" +print("Hex to dec: " + to_string(hex_to_dec(hexstr))) +print("Dec to hex: " + dec_to_hex(hex_to_dec(hexstr))) + +/* Expected output: +Hex to dec: 255 +Dec to hex: ff +*/ diff --git a/examples/test_include.fun b/examples/test_include.fun new file mode 100644 index 0000000..bf218b7 --- /dev/null +++ b/examples/test_include.fun @@ -0,0 +1,19 @@ +#!/usr/bin/env fun + +/* + * This file is part of the Fun programming language. + * https://fun-lang.xyz/ + * + * Copyright 2025 Johannes Findeisen + * Licensed under the terms of the Apache-2.0 license. + * https://opensource.org/license/apache-2-0 + * + * Added: 2025-12-18 + */ + +#include +print(dec_to_hex(255)) + +/* Expected output: +ff +*/ diff --git a/lib/hex.fun b/lib/hex.fun index 8f47924..819ce3f 100644 --- a/lib/hex.fun +++ b/lib/hex.fun @@ -87,3 +87,15 @@ fun hex_to_dec(hex) res = res * 16 + hex_val(substr(s, i, 1)) i = i + 1 return res + +fun dec_to_hex(n) + number val = n + if (val == 0) + return "0" + hexd = "0123456789abcdef" + res = "" + while val > 0 + number m = val % 16 + res = substr(hexd, m, 1) + res + val = val / 16 + return res