28 people like it.

Hex encode / decode

Performs conversions to and from hexadecimal values.

 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: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
    module Hex =
    
        open System

        [<CompiledName("ToHexDigit")>]
        let toHexDigit n =
            if n < 10 then char (n + 0x30) else char (n + 0x37)
    
        [<CompiledName("FromHexDigit")>]
        let fromHexDigit c =
            if c >= '0' && c <= '9' then int c - int '0'
            elif c >= 'A' && c <= 'F' then (int c - int 'A') + 10
            elif c >= 'a' && c <= 'f' then (int c - int 'a') + 10
            else raise <| new ArgumentException()
        
        [<CompiledName("Encode")>]
        let encode (buf:byte array) (prefix:bool) =
            let hex = Array.zeroCreate (buf.Length * 2)
            let mutable n = 0
            for i = 0 to buf.Length - 1 do
                hex.[n] <- toHexDigit ((int buf.[i] &&& 0xF0) >>> 4)
                n <- n + 1
                hex.[n] <- toHexDigit (int buf.[i] &&& 0xF)
                n <- n + 1
            if prefix then String.Concat("0x", new String(hex)) 
            else new String(hex)
            
        [<CompiledName("Decode")>]
        let decode (s:string) =
            match s with
            | null -> nullArg "s"
            | _ when s.Length = 0 -> Array.empty
            | _ ->
                let mutable len = s.Length
                let mutable i = 0
                if len >= 2 && s.[0] = '0' && (s.[1] = 'x' || s.[1] = 'X') then do
                    len <- len - 2
                    i <- i + 2
                if len % 2 <> 0 then invalidArg "s" "Invalid hex format"
                else
                    let buf = Array.zeroCreate (len / 2)
                    let mutable n = 0
                    while i < s.Length do
                        buf.[n] <- byte (((fromHexDigit s.[i]) <<< 4) ||| (fromHexDigit s.[i + 1]))
                        i <- i + 2
                        n <- n + 1
                    buf
namespace System
Multiple items
type CompiledNameAttribute =
  inherit Attribute
  new : compiledName:string -> CompiledNameAttribute
  member CompiledName : string

Full name: Microsoft.FSharp.Core.CompiledNameAttribute

--------------------
new : compiledName:string -> CompiledNameAttribute
val toHexDigit : n:int -> char

Full name: Script.Hex.toHexDigit
val n : 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 fromHexDigit : c:char -> int

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

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val raise : exn:Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type ArgumentException =
  inherit SystemException
  new : unit -> ArgumentException + 4 overloads
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Message : string
  member ParamName : string

Full name: System.ArgumentException

--------------------
ArgumentException() : unit
ArgumentException(message: string) : unit
ArgumentException(message: string, innerException: exn) : unit
ArgumentException(message: string, paramName: string) : unit
ArgumentException(message: string, paramName: string, innerException: exn) : unit
val encode : buf:byte array -> prefix:bool -> string

Full name: Script.Hex.encode
val buf : byte array
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
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val prefix : bool
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
val hex : char []
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 zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
property Array.Length: int
val mutable n : int
val i : int
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Concat([<ParamArray>] values: string []) : string
   (+0 other overloads)
String.Concat(values: Collections.Generic.IEnumerable<string>) : string
   (+0 other overloads)
String.Concat<'T>(values: Collections.Generic.IEnumerable<'T>) : string
   (+0 other overloads)
String.Concat([<ParamArray>] args: obj []) : string
   (+0 other overloads)
String.Concat(arg0: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string) : string
   (+0 other overloads)
String.Concat(arg0: obj, arg1: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string, str2: string) : string
   (+0 other overloads)
String.Concat(arg0: obj, arg1: obj, arg2: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string, str2: string, str3: string) : string
   (+0 other overloads)
val decode : s:string -> byte []

Full name: Script.Hex.decode
val s : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val nullArg : argumentName:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.nullArg
property String.Length: int
val empty<'T> : 'T []

Full name: Microsoft.FSharp.Collections.Array.empty
val mutable len : int
val mutable i : int
val invalidArg : argumentName:string -> message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidArg
val buf : byte []
Raw view Test code New version

More information

Link:http://fssnip.net/25
Posted:13 years ago
Author:Daniel Robinson
Tags: hexadecimal