Skip to content

Commit 93f6966

Browse files
author
Ricky Cheung
committed
avr: Implement our own ultoa() to ensure availability
ultoa() is not always available on some boards as it is non-standard. Provide our own copy of the function to convert nonce into string. Signed-off-by: Ricky Cheung <[email protected]>
1 parent 99ac8d5 commit 93f6966

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Arduino_Code/Arduino_Code.ino

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,40 @@ void lowercase_hex_to_bytes(char const * hexDigest, uint8_t * rawDigest) {
107107
}
108108
}
109109

110+
char* citoa(uint32_t num, char* str) {
111+
int i = 0;
112+
113+
/* Handle 0 explicitly, otherwise empty string is
114+
* printed for 0 */
115+
if (num == 0) {
116+
str[i++] = '0';
117+
str[i] = '\0';
118+
return str;
119+
}
120+
121+
// Process individual digits
122+
while (num != 0) {
123+
int rem = num % 10;
124+
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
125+
num = num / 10;
126+
}
127+
128+
str[i] = '\0'; // Append string terminator
129+
130+
// Reverse the string
131+
int start = 0;
132+
int end = i - 1;
133+
while (start < end) {
134+
char temp = str[start];
135+
str[start] = str[end];
136+
str[end] = temp;
137+
end--;
138+
start++;
139+
}
140+
141+
return str;
142+
}
143+
110144
// DUCO-S1A hasher
111145
uint32_t ducos1a() {
112146
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
@@ -123,7 +157,7 @@ uint32_t ducos1a() {
123157
uint32_t const maxNonce = job->difficulty * 100 + 1;
124158
char nonceStr[10 + 1];
125159
for (uint32_t nonce = 0; nonce < maxNonce; nonce++) {
126-
ultoa(nonce, nonceStr, 10);
160+
citoa(nonce, nonceStr);
127161

128162
uint8_t const * hash_bytes = duco_hash_try_nonce(&hash, nonceStr);
129163
if (memcmp(hash_bytes, target, SHA1_HASH_LEN) == 0) {

0 commit comments

Comments
 (0)