From 7a11e0d55547f887ad9cd5f062c97be3792a49fb Mon Sep 17 00:00:00 2001 From: Pascal Brisset Date: Thu, 15 Oct 2009 07:04:49 +0000 Subject: [PATCH] add functions to pprz transport lib --- sw/lib/ocaml/pprz.ml | 20 +++++++++++++++++++- sw/lib/ocaml/pprz.mli | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/sw/lib/ocaml/pprz.ml b/sw/lib/ocaml/pprz.ml index d74eee834e..3d17054290 100644 --- a/sw/lib/ocaml/pprz.ml +++ b/sw/lib/ocaml/pprz.ml @@ -296,7 +296,25 @@ let rec sprint_value = fun buf i _type v -> String.blit s 0 buf (i+1) n; 1 + n | (Scalar x|ArrayType x), _ -> failwith (sprintf "Pprz.sprint_value (%s)" x) - + + + +let hex_of_int_array = function + Array array -> + let n = Array.length array in + (* One integer -> 2 chars *) + let s = String.create (2*n) in + Array.iteri + (fun i dec -> + let x = int_of_value array.(i) in + assert (0 <= x && x <= 0xff); + let hex = sprintf "%02x" x in + String.blit hex 0 s (2*i) 2) + array; + s + | value -> + failwith (sprintf "Error: expecting array in Pprz.hex_of_int_array, found %s" (string_of_value value)) + exception Unknown_msg_name of string * string diff --git a/sw/lib/ocaml/pprz.mli b/sw/lib/ocaml/pprz.mli index fd0256e4e8..967e6334e3 100644 --- a/sw/lib/ocaml/pprz.mli +++ b/sw/lib/ocaml/pprz.mli @@ -72,7 +72,7 @@ val types : (string * type_descr) list type values = (string * value) list val assoc : string -> values -> value -(** Safe assoc taking into accound characters case *) +(** Safe assoc taking into accound characters case. May raise Failure ... *) val string_assoc : string -> values -> string (** May raise Not_found *) @@ -82,6 +82,9 @@ val int_assoc : string -> values -> int val int32_assoc : string -> values -> Int32.t (** May raise Not_found or Invalid_argument *) +val hex_of_int_array : value -> string +(** Returns the hexadecimal string of an array of integers *) + exception Unknown_msg_name of string * string (** [Unknown_msg_name (name, class_name)] Raised if message [name] is not found in class [class_name]. *)