arkdro avatar arkdro committed 0939fab

convert big_num to small_big_ext bytes. Byte buffer length < 256

Comments (0)

Files changed (1)

lib/ocamerl/ocamerl/tools.ml

     "6801409" = Big_int.string_of_big_int (big_int_of_chars (List.rev l1))
   )
 
-let chars_of_big_int n = failwith "chars_of_big_int not implemented yet"
+(* create bytes: len, sign, data_bytes in low-endian *)
+let small_big_chars_of_big_int num =
+  let sign, positive = match Big_int.sign_big_int num with
+    | -1 ->
+      '\001', Big_int.minus_big_int num
+    | _ ->
+      '\000', num
+  in
+  let rec f acc count x =
+    if Big_int.eq_big_int Big_int.zero_big_int x
+    then
+      (char_of_int count) :: sign :: List.rev acc
+    else
+      let low_byte = Big_int.int_of_big_int
+        (Big_int.and_big_int x (Big_int.big_int_of_int 0xFF)) in
+      let low_char = char_of_int low_byte in
+      let new_num = Big_int.shift_right_towards_zero_big_int x 8 in
+      let new_acc = low_char :: acc in
+      f new_acc (count+1) new_num
+  in
+  f [] 0 positive
+
+let chars_of_big_int num =
+  small_big_chars_of_big_int num
 
 let rec _chars_of_int v chars n =
     match n > 0 with
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.