lib: add fromHexString

Co-authored-by: lucasew <lucas59356@gmail.com>

lucasew 52cc703b 9cd5b7a8

Changed files
+43 -1
lib
+1 -1
lib/default.nix
···
info showWarnings nixpkgsVersion version isInOldestRelease
mod compare splitByAndCompare seq deepSeq lessThan add sub
functionArgs setFunctionArgs isFunction toFunction mirrorFunctionArgs
-
toHexString toBaseDigits inPureEvalMode isBool isInt pathExists
+
fromHexString toHexString toBaseDigits inPureEvalMode isBool isInt pathExists
genericClosure readFile;
inherit (self.fixedPoints) fix fix' converge extends composeExtensions
composeManyExtensions makeExtensible makeExtensibleWithCustomName;
+16
lib/tests/misc.nix
···
testAllTrue
toBaseDigits
toHexString
+
fromHexString
toInt
toIntBase10
toShellVars
···
testToHexString = {
expr = toHexString 250;
expected = "FA";
+
};
+
+
testFromHexStringFirstExample = {
+
expr = fromHexString "FF";
+
expected = 255;
+
};
+
+
testFromHexStringSecondExample = {
+
expr = fromHexString (builtins.hashString "sha256" "test");
+
expected = 9223372036854775807;
+
};
+
+
testFromHexStringWithPrefix = {
+
expr = fromHexString "0Xf";
+
expected = 15;
};
testToBaseDigits = {
+26
lib/trivial.nix
···
else k: v;
/**
+
Convert a hexadecimal string to it's integer representation.
+
+
# Type
+
+
```
+
fromHexString :: String -> [ String ]
+
```
+
+
# Examples
+
+
```nix
+
fromHexString "FF"
+
=> 255
+
+
fromHexString (builtins.hashString "sha256" "test")
+
=> 9223372036854775807
+
```
+
*/
+
fromHexString = value:
+
let
+
noPrefix = lib.strings.removePrefix "0x" (lib.strings.toLower value);
+
in let
+
parsed = builtins.fromTOML "v=0x${noPrefix}";
+
in parsed.v;
+
+
/**
Convert the given positive integer to a string of its hexadecimal
representation. For example: