2 people like it.

Convert byte array to binary string

Recursive function for converting byte array to binary string

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let convertToBinaryString bytes =
    let rec loop (acc : string) (lst : byte list) =
        match lst with
        | [] -> acc
        | _ -> loop (acc + (Convert.ToString(lst.Head, 2).PadLeft(8, '0'))) lst.Tail

    loop "" (Array.toList bytes)
val convertToBinaryString : bytes:byte [] -> string
val bytes : byte []
val loop : (string -> byte list -> string)
val acc : string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val lst : byte list
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

--------------------
type byte = System.Byte
type 'T list = List<'T>
property List.Head: byte with get
property List.Tail: byte list with get
module Array

from Microsoft.FSharp.Collections
val toList : array:'T [] -> 'T list
Raw view Test code New version

More information

Link:http://fssnip.net/82N
Posted:3 years ago
Author:Martin Lockstrom
Tags: #active patterns , #recursion