Guest User

Untitled

a guest
Oct 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. (*
  2. * Copyright (c) 2017 Yuki Ono
  3. * Licensed under the MIT License.
  4. *)
  5.  
  6. open System
  7. open System.Text
  8.  
  9. let proceduralHex (bytes:byte[]): string =
  10. let sb = new StringBuilder(bytes.Length * 2)
  11.  
  12. for b in bytes do
  13. sb.Append(b.ToString("x2")) |> ignore
  14.  
  15. sb.ToString()
  16.  
  17. let fpHex (bytes:byte[]): string =
  18. Array.map (fun (b:byte) -> b.ToString("x2")) bytes |> String.concat ""
  19.  
  20. let proceduralUnhex (hex:string): byte[] =
  21. if hex.Length % 2 <> 0 then
  22. invalidArg "hex" "hex.Length is not even"
  23.  
  24. let bytes = Array.create (hex.Length / 2) 0uy
  25.  
  26. let mutable i = 0
  27. while i < bytes.Length do
  28. bytes.[i] <- Convert.ToByte(hex.Substring(i * 2, 2), 16)
  29. i <- i + 1
  30.  
  31. bytes
  32.  
  33. let fpUnhex (hex:string): byte[] =
  34. if hex.Length % 2 <> 0 then
  35. invalidArg "hex" "hex.Length is not even"
  36.  
  37. Seq.chunkBySize 2 hex |> Seq.map (fun s -> Convert.ToByte(new String(s), 16)) |> Seq.toArray
  38.  
  39.  
  40. [<EntryPoint>]
  41. let main argv =
  42. let testBytes = [| 1uy; 2uy; 3uy; 4uy; 5uy; 11uy; 12uy; 13uy; 14uy; 15uy; 251uy; 252uy; 253uy; 254uy; 255uy |]
  43. printfn "%s" (proceduralHex testBytes)
  44. printfn "%s" (fpHex testBytes)
  45.  
  46. let testHex = "01020304050b0c0d0e0ffbfcfdfeff"
  47. printfn "%s" (String.Join(",", (proceduralUnhex testHex)))
  48. printfn "%s" (String.Join(",", (fpUnhex testHex)))
  49. Console.Read() |> ignore
  50. 0
Add Comment
Please, Sign In to add comment