gretmn102 / gretmn102/FsharpMyExtension

feat(Primitives.Numeric.Int32): add `toHex`, `toBytes`, `toBase`

Open
#4 0 comments 0 reactions 0 assignees View on GitHub
good first issue
Dominant language
F#
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

https://github.com/gretmn102/FsharpMyExtension/blob/master/src/Primitives/Numeric/Int32.fs

```fsharp
// преобразование различных систем исчисления

// System.Convert.ToString(9824, 16)
// "U+2660"
// System.Text.Encoding.Unicode.GetBytes("🂫")

// let toDec
let ofDec (toBase : int) (n:int) = System.Convert.ToString(n, toBase)
// ofDec 100 16
// 0b111
let ofDec' toBase =
cond ((=) 0)
(k [0])
(abs
>> List.unfold (function
| 0 -> None
| x -> Some(x % toBase, x / toBase))
>> List.rev)
let digits = ofDec' 10
// digits -1234
let toDec ofBase num =
List.rev num
|> List.fold (fun (k, acc) x -> ofBase * k, k * x + acc) (1, 0)
|> snd
let toDec' ofBase x = toDec ofBase (digits x)

// toDec' 8 1234
// ofDec 8 668
// on (ofDec 8) (ofDec' 8) 1234
// on (ofDec 8) (ofDec' 8) System.Int32.MaxValue //System.Int32.MinValue
// on (ofDec 8) (ofDec' 8) (System.Int32.MinValue + 1)
ofDec' 26 546 |> toDec 26 = 546

65535 |> ofDec' 256
// ***
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.