-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseConv.c
270 lines (210 loc) · 6.87 KB
/
BaseConv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*****************************************************************/
/* */
/* CASIO fx-9860G SDK Library */
/* */
/* File name : [ProjectName].c */
/* */
/* Copyright (c) 2006 CASIO COMPUTER CO., LTD. */
/* */
/*****************************************************************/
#include "fxlib.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "EasyInput.h"
// Could use an enum aswell, but since I often use the Microsoft BOOL Type I just recreated the same
typedef unsigned short BOOL;
#define TRUE 1
#define FALSE 0
// Manage the position of the cursor
typedef struct
{
unsigned short x;
unsigned short y;
} POS;
// String size to allocate
#define STR_MAX 255
#define STR_BASE 3
// Move the cursor at the correct position
void move(POS* pos, BOOL isInput)
{
if(!isInput)
{
locate(pos->x, pos->y);
}
else
{
EI_manage_config(EI_SET_COLUMN, pos->x);
EI_manage_config(EI_SET_ROW, pos->y);
}
//pos->x++; disabled x since we don't need to use different column than 1 in this case, we could make x constant
pos->y++;
}
// ------------------------- Iterative functions to implement `itoa()` function in C -------------------------
// Function to swap two numbers
void swap(char *x, char *y)
{
char t = *x; *x = *y; *y = t;
}
// Function to reverse `buffer[i…j]`
char* reverse(char *buffer, int i, int j)
{
while (i < j)
swap(&buffer[i++], &buffer[j--]);
return buffer;
}
// Iterative function to implement `itoa()` function in C
char* itoa(int value, char* buffer, int base)
{
int n, i, r;
// invalid input
if (base < 2 || base > 32)
return buffer;
// consider the absolute value of the number
n = abs(value);
i = 0;
while (n)
{
r = n % base;
if (r >= 10)
buffer[i++] = 65 + (r - 10);
else
buffer[i++] = 48 + r;
n = n / base;
}
// if the number is 0
if (i == 0)
buffer[i++] = '0';
// If the base is 10 and the value is negative, the resulting string
// is preceded with a minus sign (-)
// With any other base, value is always considered unsigned
if (value < 0 && base == 10)
buffer[i++] = '-';
buffer[i] = '\0'; // null terminate string
// reverse the string and return it
return reverse(buffer, 0, i - 1);
}
// ------------------------- End `itoa()` function in C -------------------------
// Convert arbitrary base to base 10
// - Input must be valid number in the given base
// - Base must be between 2 and 36
// - If input or base are invalid, returns 0
int ConvertTo10(const char* input, int base)
{
BOOL isNegative;
int startIndex, endIndex, digitValue, i, value;
char c;
if(base < 2 || base > 36)
return 0;
isNegative = (input[0] == '-');
startIndex = strlen(input) - 1;
endIndex = isNegative ? TRUE : FALSE;
value = 0;
digitValue = 1;
for(i = startIndex; i >= endIndex; --i)
{
c = input[i];
// Uppercase it - same could be done with std::toupper
if(c >= 'a' && c <= 'z')
c -= ('a' - 'A');
// Convert char to int value - same could be done with std::atoi
// 0-9
if(c >= '0' && c <= '9')
c -= '0';
// A-Z
else
c = c - 'A' + 10;
if(c >= base)
return 0;
// Get the base 10 value of this digit
value += c * digitValue;
// Each digit has value base^digit position - NOTE: this avoids pow
digitValue *= base;
}
if(isNegative)
value *= -1;
return value;
}
// Convert from one base to another
void ConvertBase(const char* input, int baseFrom, int baseTo, POS* displayPos)
{
char* str = malloc(STR_MAX);
itoa(ConvertTo10(input, baseFrom), str, baseTo);
move(displayPos, FALSE);
Print((unsigned char*)"[Result]");
move(displayPos, FALSE);
// If the result is longer than the max screen size
if(strlen(str) > 21)
Print((unsigned char*)"Too Long ...");
else
Print(str);
free(str);
}
int AddIn_main(int isAppli, unsigned short OptionNum)
{
unsigned int key;
char* str, *base_from, *base_to;
POS pos;
while(TRUE)
{
Bdisp_AllClr_DDVRAM();
// Initialize variables
pos.x = 1;
pos.y = 1;
str = malloc(STR_MAX);
base_from = malloc(STR_BASE);
base_to = malloc(STR_BASE);
// Init Input Lib
EI_init();
EI_manage_config(EI_SET_START_MODE, EI_NORMAL);
move(&pos, FALSE);
Print((unsigned char*)"[Input NB to convert]");
move(&pos, TRUE);
str = EI_input_string(STR_MAX, (const char*)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
move(&pos, FALSE);
Print((unsigned char*)"[Input BaseFrom]");
move(&pos, TRUE);
base_from = EI_input_string(STR_BASE, (const char*)"0123456789");
move(&pos, FALSE);
Print((unsigned char*)"[Input BaseTo]");
move(&pos, TRUE);
base_to = EI_input_string(STR_BASE, (const char*)"0123456789");
if((strlen(str) > 0) && (strlen(base_from) > 0) && (strlen(base_to) > 0))
{
ConvertBase(str, atoi(base_from), atoi(base_to), &pos);
}
free(str);
free(base_from);
free(base_to);
do
{
GetKey(&key);
} while (key != KEY_CTRL_AC);
}
return 1;
}
//****************************************************************************
//************** ****************
//************** Notice! ****************
//************** ****************
//************** Please do not change the following source. ****************
//************** ****************
//****************************************************************************
#pragma section _BR_Size
unsigned long BR_Size;
#pragma section
#pragma section _TOP
//****************************************************************************
// InitializeSystem
//
// param : isAppli : 1 = Application / 0 = eActivity
// OptionNum : Option Number (only eActivity)
//
// retval : 1 = No error / 0 = Error
//
//****************************************************************************
int InitializeSystem(int isAppli, unsigned short OptionNum)
{
return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
}
#pragma section