1 people like it.

hexdump

A more f#ish version of Robert's version http://fssnip.net/l9

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
open System
open System.IO
open System.Text

let hexdump byteCount (bytes: byte[]) =
    let totalLength = Array.length bytes
    let (|Char|Ctrl|) c = char bytes.[c] |> fun ch -> if Char.IsControl(ch) then Ctrl(c) else Char(c,ch) 
    let (|TooLong|_|) c = if c < totalLength then None else Some(c)
    let rec printLine pos =
        if pos < totalLength then
            let length = min byteCount (totalLength - pos)
            let rec printByte =
                function
                | _,0 -> printf "  |"
                | TooLong c, l -> printf "   "; printByte (c+1,l-1)
                | c,l -> printf "%02x " bytes.[c]; printByte (c+1,l-1)

            let rec printChar =
                function
                | _,0 -> printfn "|"
                | TooLong c,l -> printf "."; printChar (c+1,l-1)
                | Ctrl c,l -> printf "."; printChar (c+1,l-1)
                | Char(c,ch),l -> printf "%c" ch; printChar (c+1,l-1)

            printf "%08x  " pos
            printByte (pos, byteCount)
            printChar (pos, byteCount)

            printLine (pos + length)
    printLine 0
hexdump 8 "Hello world\nHow doing ?"B
namespace System
namespace System.IO
namespace System.Text
val hexdump : byteCount:int -> bytes:byte [] -> unit

Full name: Script.hexdump
val byteCount : int
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
val totalLength : int
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
val c : int
Multiple items
val char : value:'T -> char (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.char

--------------------
type char = Char

Full name: Microsoft.FSharp.Core.char
val ch : char
Char.IsControl(c: char) : bool
Char.IsControl(s: string, index: int) : bool
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val printLine : (int -> unit)
val pos : int
val length : int
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val printByte : (int * int -> unit)
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
active recognizer TooLong: int -> int option
val l : int
val printChar : (int * int -> unit)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
active recognizer Ctrl: int -> Choice<(int * char),int>
Multiple items
active recognizer Char: int -> Choice<(int * char),int>

--------------------
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Raw view Test code New version

More information

Link:http://fssnip.net/la
Posted:11 years ago
Author:thinkbeforecoding
Tags: binary