From 9577f26e234efb65e0f74929de0d87294231ba5f Mon Sep 17 00:00:00 2001 From: hanez Date: Thu, 18 Dec 2025 09:51:57 +0100 Subject: [PATCH] Added some more HEX (hex_to_dec()) Fun. (0.3714) --- CMakeLists.txt | 2 +- examples/conversions_showcase.fun | 0 examples/hex_example.fun | 62 ++++++++++++++++++++++++++++++ examples/random_number_example.fun | 36 ++++++++++++----- examples/test_hex_to_dec.fun | 32 +++++++++++++++ lib/hex.fun | 10 +++++ 6 files changed, 131 insertions(+), 11 deletions(-) mode change 100644 => 100755 examples/conversions_showcase.fun create mode 100755 examples/hex_example.fun create mode 100755 examples/test_hex_to_dec.fun diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f305a8..d33e6ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.37.13 LANGUAGES C) +project(fun VERSION 0.37.14 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/examples/conversions_showcase.fun b/examples/conversions_showcase.fun old mode 100644 new mode 100755 diff --git a/examples/hex_example.fun b/examples/hex_example.fun new file mode 100755 index 0000000..2b6074e --- /dev/null +++ b/examples/hex_example.fun @@ -0,0 +1,62 @@ +#!/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 + */ + +/* + * Example: Using hex utility functions with fixed values + * This demonstrates hex_to_bytes, bytes_to_hex, and hex_to_dec. + */ + +#include + +print("--- Hex utility demo (fixed values) ---") + +// Using a fixed hex string for verification +hexstr = "50a76f143d67ce173962cd2eea5a86a4" +print("Fixed Hex string:") +print(hexstr) + +print("Hex length (should be 32):") +print(to_string(len(hexstr))) + +print("Hex bytes array:") +bytes = hex_to_bytes(hexstr) +print(to_string(bytes)) + +echo("Hex dump to bytes:") +print(bytes) + +print("Back to hex from bytes:") +print(bytes_to_hex(bytes)) + +print("Hex as decimal (first 4 bytes - 0x50a76f14):") +print(to_string(hex_to_dec(substr(hexstr, 0, 8)))) + +print("Hex as decimal (all bytes):") +print(to_string(hex_to_dec(hexstr))) + +/* Expected output: +--- Hex utility demo (fixed values) --- +Fixed Hex string: +50a76f143d67ce173962cd2eea5a86a4 +Hex length (should be 32): +32 +Hex bytes array: +[array n=16] +Hex dump to bytes:[80, 167, 111, 20, 61, 103, 206, 23, 57, 98, 205, 46, 234, 90, 134, 164] +Back to hex from bytes: +50a76f143d67ce173962cd2eea5a86a4 +Hex as decimal (first 4 bytes - 0x50a76f14): +1353150228 +Hex as decimal (all bytes): +4135093009263527588 +*/ diff --git a/examples/random_number_example.fun b/examples/random_number_example.fun index 9ad5213..cdd1785 100755 --- a/examples/random_number_example.fun +++ b/examples/random_number_example.fun @@ -23,12 +23,20 @@ print("--- random_number(len) demo ---") len_bytes = 16 hexstr = random_number(len_bytes) -print("Requested bytes: " + to_string(len_bytes)) -print("Hex string: " + hexstr) -print("Hex bytes array: " + to_string(hex_to_bytes(hexstr))) -echo("Hex dump to bytes: ") +print("Requested bytes:") +print(to_string(len_bytes)) +print("Hex string:") +print(hexstr) +print("Hex bytes array:") +print(to_string(hex_to_bytes(hexstr))) +echo("Hex dump to bytes:") print(hex_to_bytes(hexstr)) -print("Hex length (should be 2*bytes = 32): " + to_string(len(hexstr))) +print("Hex as decimal (first 4 bytes):") +print(to_string(hex_to_dec(substr(hexstr, 0, 8)))) +print("Hex as decimal (all bytes):") +print(to_string(hex_to_dec(hexstr))) +print("Hex length (should be 2*bytes = 32):") +print(to_string(len(hexstr))) // Zero length returns empty string empty = random_number(0) @@ -40,11 +48,19 @@ print("32 bytes -> " + to_string(len(hex64)) + " hex chars") /* Possible output: --- random_number(len) demo --- -Requested bytes: 16 -Hex string: 8497373c7fb52c6cb7f1e1fda5bb6a60 -Hex bytes array: [array n=16] -Hex dump to bytes: [132, 151, 55, 60, 127, 181, 44, 108, 183, 241, 225, 253, 165, 187, 106, 96] -Hex length (should be 2*bytes = 32): 32 +Requested bytes: +16 +Hex string: +50a76f143d67ce173962cd2eea5a86a4 +Hex bytes array: +[array n=16] +Hex dump to bytes:[80, 167, 111, 20, 61, 103, 206, 23, 57, 98, 205, 46, 234, 90, 134, 164] +Hex as decimal (first 4 bytes): +1353150228 +Hex as decimal (all bytes): +4135093009263527588 +Hex length (should be 2*bytes = 32): +32 Empty (0 bytes) -> length: 0 value: 32 bytes -> 64 hex chars */ diff --git a/examples/test_hex_to_dec.fun b/examples/test_hex_to_dec.fun new file mode 100755 index 0000000..c655b99 --- /dev/null +++ b/examples/test_hex_to_dec.fun @@ -0,0 +1,32 @@ +#!/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("Testing hex_to_dec:") +print("0x0 -> " + to_string(hex_to_dec("0"))) +print("0xf -> " + to_string(hex_to_dec("f"))) +print("0x10 -> " + to_string(hex_to_dec("10"))) +print("0xff -> " + to_string(hex_to_dec("ff"))) +print("0x100 -> " + to_string(hex_to_dec("100"))) +print("0xdeadbeef -> " + to_string(hex_to_dec("deadbeef"))) + +/* Expected output: +Testing hex_to_dec: +0x0 -> 0 +0xf -> 15 +0x10 -> 16 +0xff -> 255 +0x100 -> 256 +0xdeadbeef -> 3735928559 +*/ diff --git a/lib/hex.fun b/lib/hex.fun index 378e9e5..8f47924 100644 --- a/lib/hex.fun +++ b/lib/hex.fun @@ -77,3 +77,13 @@ fun bytes_to_hex(arr) res = res + two_hex(arr[i]) i = i + 1 return res + +fun hex_to_dec(hex) + s = to_string(hex) + number res = 0 + number i = 0 + number n = len(s) + while i < n + res = res * 16 + hex_val(substr(s, i, 1)) + i = i + 1 + return res