Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- * Copyright (c) 2017 Yuki Ono
- * Licensed under the MIT License.
- *)
- open System
- open System.Text
- let proceduralHex (bytes:byte[]): string =
- let sb = new StringBuilder(bytes.Length * 2)
- for b in bytes do
- sb.Append(b.ToString("x2")) |> ignore
- sb.ToString()
- let fpHex (bytes:byte[]): string =
- Array.map (fun (b:byte) -> b.ToString("x2")) bytes |> String.concat ""
- let proceduralUnhex (hex:string): byte[] =
- if hex.Length % 2 <> 0 then
- invalidArg "hex" "hex.Length is not even"
- let bytes = Array.create (hex.Length / 2) 0uy
- let mutable i = 0
- while i < bytes.Length do
- bytes.[i] <- Convert.ToByte(hex.Substring(i * 2, 2), 16)
- i <- i + 1
- bytes
- let fpUnhex (hex:string): byte[] =
- if hex.Length % 2 <> 0 then
- invalidArg "hex" "hex.Length is not even"
- Seq.chunkBySize 2 hex |> Seq.map (fun s -> Convert.ToByte(new String(s), 16)) |> Seq.toArray
- [<EntryPoint>]
- let main argv =
- let testBytes = [| 1uy; 2uy; 3uy; 4uy; 5uy; 11uy; 12uy; 13uy; 14uy; 15uy; 251uy; 252uy; 253uy; 254uy; 255uy |]
- printfn "%s" (proceduralHex testBytes)
- printfn "%s" (fpHex testBytes)
- let testHex = "01020304050b0c0d0e0ffbfcfdfeff"
- printfn "%s" (String.Join(",", (proceduralUnhex testHex)))
- printfn "%s" (String.Join(",", (fpUnhex testHex)))
- Console.Read() |> ignore
- 0
Add Comment
Please, Sign In to add comment