3 people like it.

C Struct to F#

Quick function to take a wireshark 'C Structure' and turn it into a F# byte array

 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: 
let carray = 
  "char peer0_0[] = {
      0x4a, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x16, 0x00, 0x00, 0x00, 0x54, 0x44, 0x69, 0x73, 
      0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64, 
      0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 
      0x6f, 0x6e };"

let carray_to_bytes (xs : string) = 
  xs.Split '{'
  |> Seq.skip 1
  |> Seq.head
  |> fun s -> 
     let newstr = s.Replace(" ", "").Replace("\n","")
     newstr.TrimEnd [|'}';';'|]  
     |> fun ns -> ns.Split ','         
     |> Seq.map (int >> byte)
     |> Seq.toArray

let authRequest = carray_to_bytes carray
 
val carray : string

Full name: Script.carray
val carray_to_bytes : xs:string -> byte []

Full name: Script.carray_to_bytes
val xs : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
module Seq

from Microsoft.FSharp.Collections
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val head : source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.head
val s : string
val newstr : string
System.String.Replace(oldValue: string, newValue: string) : string
System.String.Replace(oldChar: char, newChar: char) : string
System.String.TrimEnd([<System.ParamArray>] trimChars: char []) : string
val ns : string
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
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<_>
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

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

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
val authRequest : byte []

Full name: Script.authRequest
Raw view New version

More information

Link:http://fssnip.net/hp
Posted:4 years ago
Author:David Klein
Tags: networking