From 804e0f112ddd608637d5528ae9e7147dc246e7dc Mon Sep 17 00:00:00 2001 From: Struan Clark Date: Fri, 3 Mar 2023 09:34:08 -0700 Subject: [PATCH] itoa --- helpers/flipbip_string.c | 40 +++++++++++++++++++++++++++++++++++++++- helpers/flipbip_string.h | 2 ++ views/flipbip_scene_1.c | 3 ++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/helpers/flipbip_string.c b/helpers/flipbip_string.c index 1885aa5dedc..7a0057a6146 100644 --- a/helpers/flipbip_string.c +++ b/helpers/flipbip_string.c @@ -27,7 +27,8 @@ * SUCH DAMAGE. */ #include "flipbip_string.h" -#include +// #include +#include char * flipbip_strtok(char *s, const char *delim) { @@ -75,4 +76,41 @@ flipbip_strtok_r(char *s, const char *delim, char **last) } while (sc != 0); } /* NOTREACHED */ +} +void +flipbip_strrev(unsigned char *str) +{ + int i; + int j; + unsigned char a; + unsigned len = strlen((const char *)str); + for (i = 0, j = len - 1; i < j; i++, j--) + { + a = str[i]; + str[i] = str[j]; + str[j] = a; + } +} +int +flipbip_itoa(int num, unsigned char* str, int len, int base) +{ + int sum = num; + int i = 0; + int digit; + if (len == 0) + return -1; + do + { + digit = sum % base; + if (digit < 0xA) + str[i++] = '0' + digit; + else + str[i++] = 'A' + digit - 0xA; + sum /= base; + }while (sum && (i < (len - 1))); + if (i == (len - 1) && sum) + return -1; + str[i] = '\0'; + flipbip_strrev(str); + return 0; } \ No newline at end of file diff --git a/helpers/flipbip_string.h b/helpers/flipbip_string.h index aafd5b6544e..83641ba3c28 100644 --- a/helpers/flipbip_string.h +++ b/helpers/flipbip_string.h @@ -1,2 +1,4 @@ char * flipbip_strtok(char *s, const char *delim); char * flipbip_strtok_r(char *s, const char *delim, char **last); +void flipbip_strrev(unsigned char *str); +int flipbip_itoa(int num, unsigned char* str, int len, int base); diff --git a/views/flipbip_scene_1.c b/views/flipbip_scene_1.c index d99adec8334..ff465e650eb 100644 --- a/views/flipbip_scene_1.c +++ b/views/flipbip_scene_1.c @@ -132,7 +132,8 @@ static void flipbip_scene_1_model_init(FlipBipScene1Model* const model, const in // Convert the seed to a hex string for (size_t i = 0; i < 64; i++) { - sprintf(seed + (i * 2), "%.2x", seedbytes[i]); + flipbip_itoa(seedbytes[i], seed + (i * 2), 2, 16); + //sprintf(seed + (i * 2), "%.2x", seedbytes[i]); } // Split the seed into parts