From bd23d89706c5852682f71921b9e150bae48bb912 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 15 Mar 2025 18:34:10 +0100 Subject: [PATCH] update math with itoa base --- module05/ex02/adc.c | 2 +- module05/ex02/header.h | 2 +- module05/ex02/math.c | 23 ++++++++++++++++------- module06/ex00/header.h | 3 +++ module06/ex00/math.c | 32 ++++++++++++++++++++++++++++++++ module06/ex00/uart.c | 12 ++++++------ 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/module05/ex02/adc.c b/module05/ex02/adc.c index 4c2990d..4cebc9c 100644 --- a/module05/ex02/adc.c +++ b/module05/ex02/adc.c @@ -25,7 +25,7 @@ void adc_print_dec(uint16_t value) { // value = calibrate_temperature(value, 5); // } char buffer[17] = {0}; - int_to_string((uint16_t)value, buffer); + int_to_string_base((uint16_t)value, buffer, 10); uart_printstr(buffer); } diff --git a/module05/ex02/header.h b/module05/ex02/header.h index 70e4ce2..df18913 100644 --- a/module05/ex02/header.h +++ b/module05/ex02/header.h @@ -25,7 +25,7 @@ extern volatile uint8_t adc_channel; void timer_1B_init(); // math.c void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits); -void int_to_string(uint64_t value, char *out); +void int_to_string_base(uint64_t value, char *buffer, uint8_t base); // adc.c void adc_init(uint8_t prescaler_value); uint16_t calibrate_temperature(uint16_t value, uint8_t speed); diff --git a/module05/ex02/math.c b/module05/ex02/math.c index 2a4be39..bafd17b 100644 --- a/module05/ex02/math.c +++ b/module05/ex02/math.c @@ -8,25 +8,34 @@ void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // nu out[num_digits] = '\0'; } -void int_to_string(uint64_t value, char *buffer) { +void int_to_string_base(uint64_t value, char *buffer, uint8_t base) { // buffer must have the right size + if (base < 2 || base > 36) { + buffer[0] = '\0'; // unsupported base + return; + } + // handle zero case if (value == 0) { buffer[0] = '0'; + buffer[1] = '\0'; return; } - uint8_t size = -1; + uint8_t size = 0; uint64_t copy = value; while (copy) { - copy /= 10; + copy /= base; size++; } + buffer[size] = '\0'; // null-terminate the string + size--; // adjust index for last digit + while (value) { - uint8_t digit = value % 10; - buffer[size] = digit + '0'; - value /= 10; + uint8_t digit = value % base; + buffer[size] = digit < 10 ? ('0' + digit) : ('A' + digit - 10); + value /= base; size--; } -} +} \ No newline at end of file diff --git a/module06/ex00/header.h b/module06/ex00/header.h index fda2646..1f2d6a2 100644 --- a/module06/ex00/header.h +++ b/module06/ex00/header.h @@ -36,6 +36,9 @@ void uart_init(); void uart_tx(char c); void uart_printstr(const char* str); void uart_printstr_endl(const char* str); +// math.c +void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits); +void int_to_string(uint64_t value, char *out); // // MACROS diff --git a/module06/ex00/math.c b/module06/ex00/math.c index 2a4be39..c445dc4 100644 --- a/module06/ex00/math.c +++ b/module06/ex00/math.c @@ -30,3 +30,35 @@ void int_to_string(uint64_t value, char *buffer) { size--; } } + +void itoa_base(uint64_t value, char *buffer, uint8_t base) { // buffer must have the right size + if (base < 2 || base > 36) { + buffer[0] = '\0'; // unsupported base + return; + } + + // handle zero case + if (value == 0) { + buffer[0] = '0'; + buffer[1] = '\0'; + return; + } + + uint8_t size = 0; + uint64_t copy = value; + + while (copy) { + copy /= base; + size++; + } + + buffer[size] = '\0'; // null-terminate the string + size--; // adjust index for last digit + + while (value) { + uint8_t digit = value % base; + buffer[size] = digit < 10 ? ('0' + digit) : ('A' + digit - 10); + value /= base; + size--; + } +} diff --git a/module06/ex00/uart.c b/module06/ex00/uart.c index 8cb3f2b..c2c1ad8 100644 --- a/module06/ex00/uart.c +++ b/module06/ex00/uart.c @@ -18,12 +18,12 @@ void uart_init() { } // char uart_rx(void) { -// while (!TEST(UCSR0A, RXC0)); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data) +// while (!TEST(UCSR0A, RXC0)); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data) // return UDR0; // 20.11.1 : get data in buffer, UDRn – USART I/O Data Register (read and write) // } void uart_tx(char c) { - while (!TEST(UCSR0A, UDRE0)); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty) + while (!TEST(UCSR0A, UDRE0)); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty) UDR0 = (unsigned char) c; // 20.11.1 : Put data into buffer, UDRn – USART I/O Data Register (read and write) } @@ -39,10 +39,10 @@ void uart_printstr_endl(const char* str) { uart_printstr("\r\n"); } -void uart_printstr_itoa_base_endl(uint64_t value) { - ; - uart_printstr(str); - uart_printstr("\r\n"); +void uart_printstr_itoa_base(uint64_t value) { + char buffer[17] = {0}; + int_to_string((uint16_t)value, buffer); + uart_printstr(buffer); } // ISR(USART_RX_vect) { // Table 12-6 : we select the code for USART Receive