Subversion Repositories HomeAutomation

Rev

Rev 1008 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1008 Rev 1011
1
 
1
 
2
#include <string>
2
#include <string>
3
 
3
 
4
/***************************************************************************
4
/***************************************************************************
5
 *   Copyright (C) 2004 by Mattias Runge                                   *
5
 *   Copyright (C) 2004 by Mattias Runge                                   *
6
 *   mattias@mrunge.se                                                     *
6
 *   mattias@mrunge.se                                                     *
7
 *                                                                         *
7
 *                                                                         *
8
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   This program is free software; you can redistribute it and/or modify  *
9
 *   it under the terms of the GNU General Public License as published by  *
9
 *   it under the terms of the GNU General Public License as published by  *
10
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   the Free Software Foundation; either version 2 of the License, or     *
11
 *   (at your option) any later version.                                   *
11
 *   (at your option) any later version.                                   *
12
 *                                                                         *
12
 *                                                                         *
13
 *   This program is distributed in the hope that it will be useful,       *
13
 *   This program is distributed in the hope that it will be useful,       *
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16
 *   GNU General Public License for more details.                          *
16
 *   GNU General Public License for more details.                          *
17
 *                                                                         *
17
 *                                                                         *
18
 *   You should have received a copy of the GNU General Public License     *
18
 *   You should have received a copy of the GNU General Public License     *
19
 *   along with this program; if not, write to the                         *
19
 *   along with this program; if not, write to the                         *
20
 *   Free Software Foundation, Inc.,                                       *
20
 *   Free Software Foundation, Inc.,                                       *
21
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22
 ***************************************************************************/
22
 ***************************************************************************/
23
#include "tools.h"
23
#include "tools.h"
24
 
24
 
25
string niceTime()
25
string niceTime()
26
{
26
{
27
    string result;
27
    string result;
28
    struct tm tmStruct;
28
    struct tm tmStruct;
29
    time_t t = time(NULL);
29
    time_t t = time(NULL);
30
    localtime_r(&t, &tmStruct);
30
    localtime_r(&t, &tmStruct);
31
 
31
 
32
    result += lpad(itos(tmStruct.tm_hour), 2, '0');
32
    result += lpad(itos(tmStruct.tm_hour), 2, '0');
33
    result += ":";
33
    result += ":";
34
    result += lpad(itos(tmStruct.tm_min), 2, '0');
34
    result += lpad(itos(tmStruct.tm_min), 2, '0');
35
    result += ":";
35
    result += ":";
36
    result += lpad(itos(tmStruct.tm_sec), 2, '0');
36
    result += lpad(itos(tmStruct.tm_sec), 2, '0');
37
 
37
 
38
    return result;
38
    return result;
-
 
39
}
-
 
40
 
-
 
41
unsigned int hex2uint(string hex)
-
 
42
{
-
 
43
    return bin2uint(hex2bin(hex));
39
}
44
}
40
 
45
 
41
unsigned int bin2uint(string bin)
46
unsigned int bin2uint(string bin)
42
{
47
{
43
    unsigned int num = 0;
48
    unsigned int num = 0;
44
 
49
 
45
    for (int n = bin.length()-1; n >= 0; n--)
50
    for (int n = bin.length()-1; n >= 0; n--)
46
    {
51
    {
47
        if (bin[n] == '1')
52
        if (bin[n] == '1')
48
            num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n));
53
            num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n));
49
    }
54
    }
50
 
55
 
51
    return num;
56
    return num;
52
}
57
}
53
 
58
 
54
string invert(string bin)
59
string invert(string bin)
55
{
60
{
56
    string inverted;
61
    string inverted;
57
 
62
 
58
    for (int n = 0; n < bin.length(); n++)
63
    for (int n = 0; n < bin.length(); n++)
59
        inverted += (bin[n] == '1' ? '0' : '1');
64
        inverted += (bin[n] == '1' ? '0' : '1');
60
 
65
 
61
    return inverted;
66
    return inverted;
62
}
67
}
63
 
68
 
64
float bin2float(string bin)
69
float bin2float(string bin)
65
{
70
{
66
    float num = 0;
71
    float num = 0;
67
 
72
 
68
    if (bin.length() == 0)
73
    if (bin.length() == 0)
69
        return 0.0f;
74
        return 0.0f;
70
 
75
 
71
    bool isNegative = false;;
76
    bool isNegative = false;;
72
 
77
 
73
    if (bin[0] == '1')
78
    if (bin[0] == '1')
74
    {
79
    {
75
        bin = invert(bin);
80
        bin = invert(bin);
76
        isNegative = true;
81
        isNegative = true;
77
    }
82
    }
78
 
83
 
79
    for (int n = bin.length()-1; n > 0; n--)
84
    for (int n = bin.length()-1; n > 0; n--)
80
    {
85
    {
81
        if (bin[n] == '1')
86
        if (bin[n] == '1')
82
            num += pow(2.0f, (int)(bin.length()-1-n));
87
            num += pow(2.0f, (int)(bin.length()-1-n));
83
    }
88
    }
84
 
89
 
85
    num /= 64.0f;
90
    num /= 64.0f;
86
 
91
 
87
    if (isNegative)
92
    if (isNegative)
88
        num = -num;
93
        num = -num;
89
 
94
 
90
    return num;
95
    return num;
91
}
96
}
92
 
97
 
93
string bin2hex(string bin)
98
string bin2hex(string bin)
94
{
99
{
95
    string hex;
100
    string hex;
96
 
101
 
97
    while (bin.size() > 0)
102
    while (bin.size() > 0)
98
    {
103
    {
99
        string part;
104
        string part;
100
 
105
 
101
        try
106
        try
102
        {
107
        {
103
            part = bin.substr(0, 4);
108
            part = bin.substr(0, 4);
104
        }
109
        }
105
        catch (std::out_of_range& e)
110
        catch (std::out_of_range& e)
106
        {
111
        {
107
            cout << "DEBUG: bin2hex exception: " << e.what() << "\n";
112
            cout << "DEBUG: bin2hex exception: " << e.what() << "\n";
108
            cout << "DEBUG: A bin=" << bin << endl;
113
            cout << "DEBUG: A bin=" << bin << endl;
109
            continue;
114
            continue;
110
        }
115
        }
111
       
116
       
112
        bin.erase(0, part.size());
117
        bin.erase(0, part.size());
113
 
118
 
114
        while (part.size() < 4)
119
        while (part.size() < 4)
115
            part = "0" + part;
120
            part = "0" + part;
116
 
121
 
117
        if (part == "0000")
122
        if (part == "0000")
118
            hex += '0';
123
            hex += '0';
119
        else if (part == "0001")
124
        else if (part == "0001")
120
            hex += '1';
125
            hex += '1';
121
        else if (part == "0010")
126
        else if (part == "0010")
122
            hex += '2';
127
            hex += '2';
123
        else if (part == "0011")
128
        else if (part == "0011")
124
            hex += '3';
129
            hex += '3';
125
        else if (part == "0100")
130
        else if (part == "0100")
126
            hex += '4';
131
            hex += '4';
127
        else if (part == "0101")
132
        else if (part == "0101")
128
            hex += '5';
133
            hex += '5';
129
        else if (part == "0110")
134
        else if (part == "0110")
130
            hex += '6';
135
            hex += '6';
131
        else if (part == "0111")
136
        else if (part == "0111")
132
            hex += '7';
137
            hex += '7';
133
        else if (part == "1000")
138
        else if (part == "1000")
134
            hex += '8';
139
            hex += '8';
135
        else if (part == "1001")
140
        else if (part == "1001")
136
            hex += '9';
141
            hex += '9';
137
        else if (part == "1010")
142
        else if (part == "1010")
138
            hex += 'a';
143
            hex += 'a';
139
        else if (part == "1011")
144
        else if (part == "1011")
140
            hex += 'b';
145
            hex += 'b';
141
        else if (part == "1100")
146
        else if (part == "1100")
142
            hex += 'c';
147
            hex += 'c';
143
        else if (part == "1101")
148
        else if (part == "1101")
144
            hex += 'd';
149
            hex += 'd';
145
        else if (part == "1110")
150
        else if (part == "1110")
146
            hex += 'e';
151
            hex += 'e';
147
        else if (part == "1111")
152
        else if (part == "1111")
148
            hex += 'f';
153
            hex += 'f';
149
    }
154
    }
150
 
155
 
151
    return hex;
156
    return hex;
152
}
157
}
153
 
158
 
154
string float2bin(float num, int length)
159
string float2bin(float num, int length)
155
{
160
{
156
    string bin;
161
    string bin;
157
 
162
 
158
    ///FIXME
163
    ///FIXME
159
 
164
 
160
    while (bin.size() < length)
165
    while (bin.size() < length)
161
        bin = "0" + bin;
166
        bin = "0" + bin;
162
 
167
 
163
    return bin;
168
    return bin;
164
}
169
}
165
 
170
 
166
string hex2bin(string hex)
171
string hex2bin(string hex)
167
{
172
{
168
    hex = strtolower(hex);
173
    hex = strtolower(hex);
169
 
174
 
170
    string bin;
175
    string bin;
171
 
176
 
172
    for (int n = 0; n < hex.length(); n++)
177
    for (int n = 0; n < hex.length(); n++)
173
    {
178
    {
174
        switch (hex[n])
179
        switch (hex[n])
175
        {
180
        {
176
            case '0': bin += "0000"; break;
181
            case '0': bin += "0000"; break;
177
            case '1': bin += "0001"; break;
182
            case '1': bin += "0001"; break;
178
            case '2': bin += "0010"; break;
183
            case '2': bin += "0010"; break;
179
            case '3': bin += "0011"; break;
184
            case '3': bin += "0011"; break;
180
            case '4': bin += "0100"; break;
185
            case '4': bin += "0100"; break;
181
            case '5': bin += "0101"; break;
186
            case '5': bin += "0101"; break;
182
            case '6': bin += "0110"; break;
187
            case '6': bin += "0110"; break;
183
            case '7': bin += "0111"; break;
188
            case '7': bin += "0111"; break;
184
            case '8': bin += "1000"; break;
189
            case '8': bin += "1000"; break;
185
            case '9': bin += "1001"; break;
190
            case '9': bin += "1001"; break;
186
            case 'a': bin += "1010"; break;
191
            case 'a': bin += "1010"; break;
187
            case 'b': bin += "1011"; break;
192
            case 'b': bin += "1011"; break;
188
            case 'c': bin += "1100"; break;
193
            case 'c': bin += "1100"; break;
189
            case 'd': bin += "1101"; break;
194
            case 'd': bin += "1101"; break;
190
            case 'e': bin += "1110"; break;
195
            case 'e': bin += "1110"; break;
191
            case 'f': bin += "1111"; break;
196
            case 'f': bin += "1111"; break;
192
        }
197
        }
193
    }
198
    }
194
 
199
 
195
    return bin;
200
    return bin;
196
}
201
}
197
 
202
 
198
string uint2bin(unsigned int num, int length)
203
string uint2bin(unsigned int num, int length)
199
{
204
{
200
    string bin;
205
    string bin;
201
 
206
 
202
    while (num)
207
    while (num)
203
    {
208
    {
204
        bin = char((num&1)+'0') + bin;
209
        bin = char((num&1)+'0') + bin;
205
        num >>= 1;
210
        num >>= 1;
206
    }
211
    }
207
 
212
 
208
    while (bin.size() < length)
213
    while (bin.size() < length)
209
        bin = "0" + bin;
214
        bin = "0" + bin;
210
 
215
 
211
    return bin;
216
    return bin;
212
}
217
}
213
 
218
 
214
string uint2hex(unsigned int num, int length)
219
string uint2hex(unsigned int num, int length)
215
{
220
{
216
    return bin2hex(uint2bin(num, length));
221
    return bin2hex(uint2bin(num, length));
217
}
222
}
218
 
223
 
219
vector<string> explode(string delimiter, string str)
224
vector<string> explode(string delimiter, string str)
220
{
225
{
221
    vector<string> parts;
226
    vector<string> parts;
222
    string::size_type startPos = 0;
227
    string::size_type startPos = 0;
223
    string::size_type endPos = 0;
228
    string::size_type endPos = 0;
224
 
229
 
225
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
230
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
226
    {
231
    {
227
        try
232
        try
228
        {
233
        {
229
            parts.push_back(str.substr(startPos, endPos-startPos));
234
            parts.push_back(str.substr(startPos, endPos-startPos));
230
        }
235
        }
231
        catch (std::out_of_range& e)
236
        catch (std::out_of_range& e)
232
        {
237
        {
233
            cout << "DEBUG: explode exception: " << e.what() << "\n";
238
            cout << "DEBUG: explode exception: " << e.what() << "\n";
234
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
239
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
235
            continue;
240
            continue;
236
        }
241
        }
237
       
242
       
238
        startPos = endPos+delimiter.length();
243
        startPos = endPos+delimiter.length();
239
    }
244
    }
240
 
245
 
241
    if (startPos < str.length())
246
    if (startPos < str.length())
242
    {
247
    {
243
        try
248
        try
244
        {
249
        {
245
            parts.push_back(str.substr(startPos, str.length()-startPos));
250
            parts.push_back(str.substr(startPos, str.length()-startPos));
246
        }
251
        }
247
        catch (std::out_of_range& e)
252
        catch (std::out_of_range& e)
248
        {
253
        {
249
            cout << "DEBUG: explode exception: " << e.what() << "\n";
254
            cout << "DEBUG: explode exception: " << e.what() << "\n";
250
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
255
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
251
        }
256
        }
252
    }
257
    }
253
 
258
 
254
    return parts;
259
    return parts;
255
}
260
}
256
 
261
 
257
string strtoupper(string s)
262
string strtoupper(string s)
258
{
263
{
259
    for (unsigned int n = 0; n < s.size(); n++)
264
    for (unsigned int n = 0; n < s.size(); n++)
260
        s[n] = (char)toupper(s[n]);
265
        s[n] = (char)toupper(s[n]);
261
 
266
 
262
    return s;
267
    return s;
263
}
268
}
264
 
269
 
265
string strtolower(string s)
270
string strtolower(string s)
266
{
271
{
267
    for (unsigned int n = 0; n < s.size(); n++)
272
    for (unsigned int n = 0; n < s.size(); n++)
268
        s[n] = (char)tolower(s[n]);
273
        s[n] = (char)tolower(s[n]);
269
 
274
 
270
    return s;
275
    return s;
271
}
276
}
272
 
277
 
273
string trim(string s)
278
string trim(string s)
274
{
279
{
275
    return trim(s, ' ');
280
    return trim(s, ' ');
276
}
281
}
277
 
282
 
278
string trim(const string s, char c)
283
string trim(const string s, char c)
279
{
284
{
280
    string result = s;
285
    string result = s;
281
    while (result[0] == c)
286
    while (result[0] == c)
282
        result.erase(0, 1);
287
        result.erase(0, 1);
283
 
288
 
284
    while (result[result.length()-1] == c)
289
    while (result[result.length()-1] == c)
285
        result.erase(result.length()-1, 1);
290
        result.erase(result.length()-1, 1);
286
 
291
 
287
    return result;
292
    return result;
288
}
293
}
289
 
294
 
290
int stoi(const string s)
295
int stoi(const string s)
291
{
296
{
292
    istringstream in(s);
297
    istringstream in(s);
293
    int i;
298
    int i;
294
    in >> i;
299
    in >> i;
295
    return i;
300
    return i;
296
}
301
}
297
 
302
 
298
string itos(int i)
303
string itos(int i)
299
{
304
{
300
    ostringstream out;
305
    ostringstream out;
301
    out << i;
306
    out << i;
302
    return out.str();
307
    return out.str();
303
}
308
}
304
 
309
 
305
unsigned int stou(const string s)
310
unsigned int stou(const string s)
306
{
311
{
307
    istringstream in(s);
312
    istringstream in(s);
308
    unsigned int i;
313
    unsigned int i;
309
    in >> i;
314
    in >> i;
310
    return i;
315
    return i;
311
}
316
}
312
 
317
 
313
string utos(unsigned int i)
318
string utos(unsigned int i)
314
{
319
{
315
    ostringstream out;
320
    ostringstream out;
316
    out << i;
321
    out << i;
317
    return out.str();
322
    return out.str();
318
}
323
}
319
 
324
 
320
float stof(const string s)
325
float stof(const string s)
321
{
326
{
322
    istringstream in(s);
327
    istringstream in(s);
323
    float f;
328
    float f;
324
    in >> f;
329
    in >> f;
325
    return f;
330
    return f;
326
}
331
}
327
 
332
 
328
string ftos(float f)
333
string ftos(float f)
329
{
334
{
330
    ostringstream out;
335
    ostringstream out;
331
    out << f;
336
    out << f;
332
    return out.str();
337
    return out.str();
333
}
338
}
334
 
339
 
335
string str_replace(string search, string replace, string str)
340
string str_replace(string search, string replace, string str)
336
{
341
{
337
    vector<string> parts = explode(search, str);
342
    vector<string> parts = explode(search, str);
338
 
343
 
339
    string result;
344
    string result;
340
    for (int n = 0; n < parts.size(); n++)
345
    for (int n = 0; n < parts.size(); n++)
341
    {
346
    {
342
        result += parts[n];
347
        result += parts[n];
343
 
348
 
344
        if (n < parts.size()-1)
349
        if (n < parts.size()-1)
345
            result += replace;
350
            result += replace;
346
    }
351
    }
347
 
352
 
348
    return result;
353
    return result;
349
}
354
}
350
 
355
 
351
string file_get_contents(string filename)
356
string file_get_contents(string filename)
352
{
357
{
353
    ifstream srcfile(filename.c_str());
358
    ifstream srcfile(filename.c_str());
354
 
359
 
355
    if (!srcfile.is_open())
360
    if (!srcfile.is_open())
356
    {
361
    {
357
        srcfile.close();
362
        srcfile.close();
358
        return "";
363
        return "";
359
    }
364
    }
360
 
365
 
361
    string buffer = "";
366
    string buffer = "";
362
    string line;
367
    string line;
363
 
368
 
364
    while (!srcfile.eof())
369
    while (!srcfile.eof())
365
    {
370
    {
366
        getline(srcfile, line);
371
        getline(srcfile, line);
367
 
372
 
368
        if (srcfile.eof())
373
        if (srcfile.eof())
369
            break;
374
            break;
370
       
375
       
371
        buffer += line + "\n";
376
        buffer += line + "\n";
372
    };
377
    };
373
 
378
 
374
    srcfile.close();
379
    srcfile.close();
375
 
380
 
376
    return buffer;
381
    return buffer;
377
}
382
}
378
 
383
 
379
bool file_exists(string filename)
384
bool file_exists(string filename)
380
{
385
{
381
    ifstream file(filename.c_str());
386
    ifstream file(filename.c_str());
382
    if (file.is_open())
387
    if (file.is_open())
383
    {
388
    {
384
        file.close();
389
        file.close();
385
        return true;
390
        return true;
386
    }
391
    }
387
 
392
 
388
    return false;
393
    return false;
389
}
394
}
390
 
395
 
391
string escape(string in)
396
string escape(string in)
392
{
397
{
393
    string out;
398
    string out;
394
 
399
 
395
    for (int n = 0; n < in.length(); n++)
400
    for (int n = 0; n < in.length(); n++)
396
    {
401
    {
397
        switch (in[n])
402
        switch (in[n])
398
        {
403
        {
399
            case '\r':
404
            case '\r':
400
                break;
405
                break;
401
 
406
 
402
            case '\n':
407
            case '\n':
403
                out += "\\n";
408
                out += "\\n";
404
                break;
409
                break;
405
 
410
 
406
            case '\'':
411
            case '\'':
407
                out += "\\'";
412
                out += "\\'";
408
                break;
413
                break;
409
 
414
 
410
            case '"':
415
            case '"':
411
                out += "\\\"";
416
                out += "\\\"";
412
                break;
417
                break;
413
 
418
 
414
            default:
419
            default:
415
                out += in[n];
420
                out += in[n];
416
                break;
421
                break;
417
        }
422
        }
418
    }
423
    }
419
 
424
 
420
    return out;
425
    return out;
421
}
426
}
422
 
427
 
423
string rpad(string in, int length, char c)
428
string rpad(string in, int length, char c)
424
{
429
{
425
    while (in.length() < length)
430
    while (in.length() < length)
426
    {
431
    {
427
        in += c;
432
        in += c;
428
    }
433
    }
429
 
434
 
430
    return in;
435
    return in;
431
}
436
}
432
 
437
 
433
string lpad(string in, int length, char c)
438
string lpad(string in, int length, char c)
434
{
439
{
435
    while (in.length() < length)
440
    while (in.length() < length)
436
    {
441
    {
437
        in = c + in;
442
        in = c + in;
438
    }
443
    }
439
 
444
 
440
    return in;
445
    return in;
441
}
446
}
442
 
447
 
443
 
448
 
444
// FileTools
449
// FileTools
445
 
450
 
446
vector<string> FileTools::GetDirList(string path)
451
vector<string> FileTools::GetDirList(string path)
447
{
452
{
448
    DIR *dir = opendir(path.c_str());
453
    DIR *dir = opendir(path.c_str());
449
    vector<string> list;
454
    vector<string> list;
450
 
455
 
451
    if (!dir)
456
    if (!dir)
452
        return list;
457
        return list;
453
 
458
 
454
    dirent *d;
459
    dirent *d;
455
    while ((d = readdir(dir)))
460
    while ((d = readdir(dir)))
456
    {
461
    {
457
        if (d->d_name[0] == '.')
462
        if (d->d_name[0] == '.')
458
            continue;
463
            continue;
459
 
464
 
460
        list.push_back(d->d_name);
465
        list.push_back(d->d_name);
461
    }
466
    }
462
    closedir(dir);
467
    closedir(dir);
463
 
468
 
464
    return list;
469
    return list;
465
}
470
}
466
 
471
 
467
string FileTools::GetUniqeFilename(string path, string prefix, string postfix)
472
string FileTools::GetUniqeFilename(string path, string prefix, string postfix)
468
{
473
{
469
    time_t now;
474
    time_t now;
470
    time(&now);
475
    time(&now);
471
 
476
 
472
    string filename = path + prefix + itos(now);
477
    string filename = path + prefix + itos(now);
473
 
478
 
474
    int index = 0;
479
    int index = 0;
475
    while (1)
480
    while (1)
476
    {
481
    {
477
        string file = filename + itos(index) + postfix;
482
        string file = filename + itos(index) + postfix;
478
        if (!Exist(file))
483
        if (!Exist(file))
479
            break;
484
            break;
480
        index++;
485
        index++;
481
    }
486
    }
482
 
487
 
483
    filename += itos(index) + postfix;
488
    filename += itos(index) + postfix;
484
 
489
 
485
    return filename;
490
    return filename;
486
}
491
}
487
 
492
 
488
bool FileTools::SaveFile(string filepath, string data)
493
bool FileTools::SaveFile(string filepath, string data)
489
{
494
{
490
    ofstream destfile(filepath.c_str());
495
    ofstream destfile(filepath.c_str());
491
    if (!destfile.is_open())
496
    if (!destfile.is_open())
492
    {
497
    {
493
        destfile.close();
498
        destfile.close();
494
        return false;
499
        return false;
495
    }
500
    }
496
 
501
 
497
    destfile << data;
502
    destfile << data;
498
 
503
 
499
    destfile.close();
504
    destfile.close();
500
    return true;
505
    return true;
501
}
506
}
502
 
507
 
503
string FileTools::Get(string src)
508
string FileTools::Get(string src)
504
{
509
{
505
    ifstream srcfile(src.c_str());
510
    ifstream srcfile(src.c_str());
506
    if (!srcfile.is_open())
511
    if (!srcfile.is_open())
507
    {
512
    {
508
        srcfile.close();
513
        srcfile.close();
509
        return "";
514
        return "";
510
    }
515
    }
511
 
516
 
512
    string buffer = "";
517
    string buffer = "";
513
    string line;
518
    string line;
514
    while (!srcfile.eof())
519
    while (!srcfile.eof())
515
    {
520
    {
516
        getline(srcfile, line);
521
        getline(srcfile, line);
517
        if (srcfile.eof())
522
        if (srcfile.eof())
518
            break;
523
            break;
519
        buffer += line + "\n";
524
        buffer += line + "\n";
520
    };
525
    };
521
 
526
 
522
    srcfile.close();
527
    srcfile.close();
523
 
528
 
524
    return buffer;
529
    return buffer;
525
}
530
}
526
 
531
 
527
bool FileTools::Copy(string src, string dest)
532
bool FileTools::Copy(string src, string dest)
528
{
533
{
529
    ifstream srcfile(src.c_str());
534
    ifstream srcfile(src.c_str());
530
    if (!srcfile.is_open())
535
    if (!srcfile.is_open())
531
    {
536
    {
532
        srcfile.close();
537
        srcfile.close();
533
        return false;
538
        return false;
534
    }
539
    }
535
 
540
 
536
    ofstream destfile(dest.c_str());
541
    ofstream destfile(dest.c_str());
537
    if (!destfile.is_open())
542
    if (!destfile.is_open())
538
    {
543
    {
539
        destfile.close();
544
        destfile.close();
540
        srcfile.close();
545
        srcfile.close();
541
        return false;
546
        return false;
542
    }
547
    }
543
 
548
 
544
    string line;
549
    string line;
545
    while (!srcfile.eof())
550
    while (!srcfile.eof())
546
    {
551
    {
547
        getline(srcfile, line);
552
        getline(srcfile, line);
548
        if (srcfile.eof())
553
        if (srcfile.eof())
549
            break;
554
            break;
550
        destfile << line << endl;
555
        destfile << line << endl;
551
    };
556
    };
552
 
557
 
553
    destfile.close();
558
    destfile.close();
554
    srcfile.close();
559
    srcfile.close();
555
 
560
 
556
    return true;
561
    return true;
557
}
562
}
558
 
563
 
559
bool FileTools::Exist(string filename)
564
bool FileTools::Exist(string filename)
560
{
565
{
561
    ifstream file(filename.c_str());
566
    ifstream file(filename.c_str());
562
    if (file.is_open())
567
    if (file.is_open())
563
    {
568
    {
564
        file.close();
569
        file.close();
565
        return true;
570
        return true;
566
    }
571
    }
567
 
572
 
568
    return false;
573
    return false;
569
}
574
}
570
 
575
 
571
 
576
 
572
 
577
 
573
 
578
 
574
 
579
 
575
/*
580
/*
576
void bd()
581
void bd()
577
{
582
{
578
         int n,b=0,a[6],i=0,t=0;
583
         int n,b=0,a[6],i=0,t=0;
579
         clrscr();
584
         clrscr();
580
         printf("Conversion from Binary to Decimal\n");
585
         printf("Conversion from Binary to Decimal\n");
581
         printf("Enter Binary Number\n");
586
         printf("Enter Binary Number\n");
582
         scanf("%d",&n);
587
         scanf("%d",&n);
583
         while(n!=0)
588
         while(n!=0)
584
         {
589
         {
585
          a[i]=n%10;
590
          a[i]=n%10;
586
          n=n/10;
591
          n=n/10;
587
          if(a[i]!=1 && a[i]!=0)
592
          if(a[i]!=1 && a[i]!=0)
588
           t=1;
593
           t=1;
589
          i++;
594
          i++;
590
         }
595
         }
591
         a[i]=2;
596
         a[i]=2;
592
         n=1;
597
         n=1;
593
         for(i=0;a[i]!=2;i++)
598
         for(i=0;a[i]!=2;i++)
594
         {
599
         {
595
          b=b+a[i]*n;
600
          b=b+a[i]*n;
596
          n=n*2;
601
          n=n*2;
597
         }
602
         }
598
         if(t==0)
603
         if(t==0)
599
          printf("Decimal Equivalent=%d",B);
604
          printf("Decimal Equivalent=%d",B);
600
         else if(t==1)
605
         else if(t==1)
601
          printf("Entered number is not binary.");
606
          printf("Entered number is not binary.");
602
 
607
 
603
}
608
}
604
 
609
 
605
void db()
610
void db()
606
{
611
{
607
         int dec,bin,n,i=0,a[10];
612
         int dec,bin,n,i=0,a[10];
608
         clrscr();
613
         clrscr();
609
         printf("Conversion from Decimal to Binary\n");
614
         printf("Conversion from Decimal to Binary\n");
610
         printf("Input decimal no.");
615
         printf("Input decimal no.");
611
         scanf("%d",&dec);
616
         scanf("%d",&dec);
612
         do
617
         do
613
         {
618
         {
614
         a[i]=dec%2;
619
         a[i]=dec%2;
615
         dec=dec/2;
620
         dec=dec/2;
616
         i++;
621
         i++;
617
         }while(dec!=0);
622
         }while(dec!=0);
618
         for(n=i-1;n>=0;n--)
623
         for(n=i-1;n>=0;n--)
619
         printf("%d",a[n]);
624
         printf("%d",a[n]);
620
}
625
}
621
 
626
 
622
void doc()
627
void doc()
623
{
628
{
624
         int n,i,a[10];
629
         int n,i,a[10];
625
         clrscr();
630
         clrscr();
626
         printf("Conversion from Decimal to Octal\n");
631
         printf("Conversion from Decimal to Octal\n");
627
         printf("Enter a Decimal number\n");
632
         printf("Enter a Decimal number\n");
628
         scanf("%d",&n);
633
         scanf("%d",&n);
629
         i=0;
634
         i=0;
630
         printf("Octal equavalent of %d is ",n);
635
         printf("Octal equavalent of %d is ",n);
631
         while(n!=0)
636
         while(n!=0)
632
         {
637
         {
633
          a[i]=n%8;
638
          a[i]=n%8;
634
          n=n/8;
639
          n=n/8;
635
          i++;
640
          i++;
636
         }
641
         }
637
         i--;
642
         i--;
638
         for(;i>=0;i--)
643
         for(;i>=0;i--)
639
          printf("%d",a[i]);
644
          printf("%d",a[i]);
640
}
645
}
641
 
646
 
642
void dh(long n)
647
void dh(long n)
643
{
648
{
644
         long i;
649
         long i;
645
         if(n>0)
650
         if(n>0)
646
         {
651
         {
647
          i=n%16;
652
          i=n%16;
648
          n=n/16;
653
          n=n/16;
649
          dh(n);
654
          dh(n);
650
          if(i>=10)
655
          if(i>=10)
651
          {
656
          {
652
           switch(i)
657
           switch(i)
653
           {
658
           {
654
            case 10:
659
            case 10:
655
             printf("A");
660
             printf("A");
656
             break;
661
             break;
657
            case 11:
662
            case 11:
658
             printf("B");
663
             printf("B");
659
             break;
664
             break;
660
            case 12:
665
            case 12:
661
             printf("C");
666
             printf("C");
662
             break;
667
             break;
663
            case 13:
668
            case 13:
664
             printf("D");
669
             printf("D");
665
             break;
670
             break;
666
            case 14:
671
            case 14:
667
             printf("E");
672
             printf("E");
668
             break;
673
             break;
669
            case 15:
674
            case 15:
670
             printf("F");
675
             printf("F");
671
             break;
676
             break;
672
           }
677
           }
673
          }
678
          }
674
          else
679
          else
675
           printf("%ld",i);
680
           printf("%ld",i);
676
         }
681
         }
677
}
682
}
678
 
683
 
679
void od()
684
void od()
680
{
685
{
681
         unsigned long n,i=0,a,p=1,t=0;
686
         unsigned long n,i=0,a,p=1,t=0;
682
         clrscr();
687
         clrscr();
683
         printf("Conversion from Octal to Decimal\n");
688
         printf("Conversion from Octal to Decimal\n");
684
         printf("Enter a Octal number\n");
689
         printf("Enter a Octal number\n");
685
         scanf("%ld",&n);
690
         scanf("%ld",&n);
686
         i=0;
691
         i=0;
687
         printf("Decimal equavalent of %ld",n);
692
         printf("Decimal equavalent of %ld",n);
688
         while(n!=0)
693
         while(n!=0)
689
         {
694
         {
690
          a=n%10;
695
          a=n%10;
691
          if(a>7)
696
          if(a>7)
692
           t=1;
697
           t=1;
693
          n=n/10;
698
          n=n/10;
694
          i=i+a*p;
699
          i=i+a*p;
695
          p=p*8;
700
          p=p*8;
696
         }
701
         }
697
         if(t==0)
702
         if(t==0)
698
          printf("= %ld",i);
703
          printf("= %ld",i);
699
         else if(t==1)
704
         else if(t==1)
700
          printf(" can't be calculated because it is not an Octal Number.\n");
705
          printf(" can't be calculated because it is not an Octal Number.\n");
701
}
706
}
702
 
707
 
703
void ob()
708
void ob()
704
{
709
{
705
         int n,a[6],i=0,t=0;
710
         int n,a[6],i=0,t=0;
706
         clrscr();
711
         clrscr();
707
         printf("Convertion from Octal to Binary.\n");
712
         printf("Convertion from Octal to Binary.\n");
708
         printf("Enter an Octal Number.\n");
713
         printf("Enter an Octal Number.\n");
709
         scanf("%d",&n);
714
         scanf("%d",&n);
710
         while(n!=0)
715
         while(n!=0)
711
         {
716
         {
712
          a[i]=n%10;
717
          a[i]=n%10;
713
          n=n/10;
718
          n=n/10;
714
          if(a[i]>7)
719
          if(a[i]>7)
715
           t=1;
720
           t=1;
716
          i++;
721
          i++;
717
         }
722
         }
718
         i--;
723
         i--;
719
         if(t==0)
724
         if(t==0)
720
          for(;i>=0;i--)
725
          for(;i>=0;i--)
721
          {
726
          {
722
           switch(a[i])
727
           switch(a[i])
723
           {
728
           {
724
            case 0:
729
            case 0:
725
             printf("000");
730
             printf("000");
726
             break;
731
             break;
727
            case 1:
732
            case 1:
728
             printf("001");
733
             printf("001");
729
             break;
734
             break;
730
            case 2:
735
            case 2:
731
             printf("010");
736
             printf("010");
732
             break;
737
             break;
733
            case 3:
738
            case 3:
734
             printf("011");
739
             printf("011");
735
             break;
740
             break;
736
            case 4:
741
            case 4:
737
             printf("100");
742
             printf("100");
738
             break;
743
             break;
739
            case 5:
744
            case 5:
740
             printf("101");
745
             printf("101");
741
             break;
746
             break;
742
            case 6:
747
            case 6:
743
             printf("110");
748
             printf("110");
744
             break;
749
             break;
745
            case 7:
750
            case 7:
746
             printf("111");
751
             printf("111");
747
             break;
752
             break;
748
           }
753
           }
749
          }
754
          }
750
         if(t==1)
755
         if(t==1)
751
          printf("Not a Octal number\n");
756
          printf("Not a Octal number\n");
752
}
757
}
753
 
758
 
754
void bo()
759
void bo()
755
{
760
{
756
         int i=0,a[5],t=0;
761
         int i=0,a[5],t=0;
757
         long int n;
762
         long int n;
758
         clrscr();
763
         clrscr();
759
         printf("Convertion From Binary to Octal\n");
764
         printf("Convertion From Binary to Octal\n");
760
         printf("Enter a Binary number\n");
765
         printf("Enter a Binary number\n");
761
         scanf("%ld",&n);
766
         scanf("%ld",&n);
762
         while(n!=0)
767
         while(n!=0)
763
         {
768
         {
764
          a[i]=n%1000;
769
          a[i]=n%1000;
765
          n=n/1000;
770
          n=n/1000;
766
          if(a[i]>111)
771
          if(a[i]>111)
767
           t=1;
772
           t=1;
768
          i++;
773
          i++;
769
         }
774
         }
770
         i--;
775
         i--;
771
         if(t==0)
776
         if(t==0)
772
          for(;i>=0;i--)
777
          for(;i>=0;i--)
773
          {
778
          {
774
           switch(a[i])
779
           switch(a[i])
775
           {
780
           {
776
            case 0:
781
            case 0:
777
             printf("0");
782
             printf("0");
778
             break;
783
             break;
779
            case 1:
784
            case 1:
780
             printf("1");
785
             printf("1");
781
             break;
786
             break;
782
            case 10:
787
            case 10:
783
             printf("2");
788
             printf("2");
784
             break;
789
             break;
785
            case 11:
790
            case 11:
786
             printf("3");
791
             printf("3");
787
             break;
792
             break;
788
            case 100:
793
            case 100:
789
             printf("4");
794
             printf("4");
790
             break;
795
             break;
791
            case 101:
796
            case 101:
792
             printf("5");
797
             printf("5");
793
             break;
798
             break;
794
            case 110:
799
            case 110:
795
             printf("6");
800
             printf("6");
796
             break;
801
             break;
797
            case 111:
802
            case 111:
798
             printf("7");
803
             printf("7");
799
             break;
804
             break;
800
            default:
805
            default:
801
   printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
806
   printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
802
             break;
807
             break;
803
           }
808
           }
804
          }
809
          }
805
         if(t==1)
810
         if(t==1)
806
          printf("Number is not Binary\n");
811
          printf("Number is not Binary\n");
807
}
812
}
808
 
813
 
809
void bh()
814
void bh()
810
{
815
{
811
         int i=0,a[5],t=0;
816
         int i=0,a[5],t=0;
812
         long int n;
817
         long int n;
813
         clrscr();
818
         clrscr();
814
         printf("Convertion from Binary to Hexadecimal\n");
819
         printf("Convertion from Binary to Hexadecimal\n");
815
         printf("Enter a Binary number\n");
820
         printf("Enter a Binary number\n");
816
         scanf("%ld",&n);
821
         scanf("%ld",&n);
817
         while(n!=0)
822
         while(n!=0)
818
         {
823
         {
819
          a[i]=n%10000;
824
          a[i]=n%10000;
820
          n=n/10000;
825
          n=n/10000;
821
          if(a[i]>1111)
826
          if(a[i]>1111)
822
           t=1;
827
           t=1;
823
          i++;
828
          i++;
824
         }
829
         }
825
         i--;
830
         i--;
826
         if(t==0)
831
         if(t==0)
827
          for(;i>=0;i--)
832
          for(;i>=0;i--)
828
          {
833
          {
829
           switch(a[i])
834
           switch(a[i])
830
           {
835
           {
831
            case 0:
836
            case 0:
832
             printf("0");
837
             printf("0");
833
             break;
838
             break;
834
            case 1:
839
            case 1:
835
             printf("1");
840
             printf("1");
836
             break;
841
             break;
837
            case 10:
842
            case 10:
838
             printf("2");
843
             printf("2");
839
             break;
844
             break;
840
            case 11:
845
            case 11:
841
             printf("3");
846
             printf("3");
842
             break;
847
             break;
843
            case 100:
848
            case 100:
844
             printf("4");
849
             printf("4");
845
             break;
850
             break;
846
            case 101:
851
            case 101:
847
             printf("5");
852
             printf("5");
848
             break;
853
             break;
849
            case 110:
854
            case 110:
850
             printf("6");
855
             printf("6");
851
             break;
856
             break;
852
            case 111:
857
            case 111:
853
             printf("7");
858
             printf("7");
854
             break;
859
             break;
855
            case 1000:
860
            case 1000:
856
             printf("8");
861
             printf("8");
857
             break;
862
             break;
858
            case 1001:
863
            case 1001:
859
             printf("9");
864
             printf("9");
860
             break;
865
             break;
861
            case 1010:
866
            case 1010:
862
             printf("A");
867
             printf("A");
863
             break;
868
             break;
864
            case 1011:
869
            case 1011:
865
             printf("B");
870
             printf("B");
866
             break;
871
             break;
867
            case 1100:
872
            case 1100:
868
             printf("C");
873
             printf("C");
869
             break;
874
             break;
870
            case 1101:
875
            case 1101:
871
             printf("D");
876
             printf("D");
872
             break;
877
             break;
873
            case 1110:
878
            case 1110:
874
             printf("E");
879
             printf("E");
875
             break;
880
             break;
876
            case 1111:
881
            case 1111:
877
             printf("F");
882
             printf("F");
878
             break;
883
             break;
879
            default:
884
            default:
880
     printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
885
     printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
881
             break;
886
             break;
882
           }
887
           }
883
          }
888
          }
884
         if(t==1)
889
         if(t==1)
885
          printf("Number is not Binary\n");
890
          printf("Number is not Binary\n");
886
}
891
}
887
 
892
 
888
void hb()
893
void hb()
889
{
894
{
890
         int i;
895
         int i;
891
         char s[20];
896
         char s[20];
892
         clrscr();
897
         clrscr();
893
         printf("Convertion from Hexadecimal to Binary\n");
898
         printf("Convertion from Hexadecimal to Binary\n");
894
         printf("Enter a Hexadecimal number\n");
899
         printf("Enter a Hexadecimal number\n");
895
         scanf("%s",s);
900
         scanf("%s",s);
896
         //gets(s);
901
         //gets(s);
897
         printf("Binary Equivalent=");
902
         printf("Binary Equivalent=");
898
         for(i=0;s[i]!=NULL;i++)
903
         for(i=0;s[i]!=NULL;i++)
899
         {
904
         {
900
          switch(s[i])
905
          switch(s[i])
901
           {
906
           {
902
            case '0':
907
            case '0':
903
             printf("0000");
908
             printf("0000");
904
             break;
909
             break;
905
            case '1':
910
            case '1':
906
             printf("0001");
911
             printf("0001");
907
             break;
912
             break;
908
            case '2':
913
            case '2':
909
             printf("0010");
914
             printf("0010");
910
             break;
915
             break;
911
            case '3':
916
            case '3':
912
             printf("0011");
917
             printf("0011");
913
             break;
918
             break;
914
            case '4':
919
            case '4':
915
             printf("0100");
920
             printf("0100");
916
             break;
921
             break;
917
            case '5':
922
            case '5':
918
             printf("0101");
923
             printf("0101");
919
             break;
924
             break;
920
            case '6':
925
            case '6':
921
             printf("0110");
926
             printf("0110");
922
             break;
927
             break;
923
            case '7':
928
            case '7':
924
             printf("0111");
929
             printf("0111");
925
             break;
930
             break;
926
            case '8':
931
            case '8':
927
             printf("1000");
932
             printf("1000");
928
             break;
933
             break;
929
            case '9':
934
            case '9':
930
             printf("1001");
935
             printf("1001");
931
             break;
936
             break;
932
            case 'a':
937
            case 'a':
933
            case 'A':
938
            case 'A':
934
             printf("1010");
939
             printf("1010");
935
             break;
940
             break;
936
            case 'b':
941
            case 'b':
937
            case 'B':
942
            case 'B':
938
             printf("1011");
943
             printf("1011");
939
             break;
944
             break;
940
            case 'c':
945
            case 'c':
941
            case 'C':
946
            case 'C':
942
             printf("1100");
947
             printf("1100");
943
             break;
948
             break;
944
            case 'd':
949
            case 'd':
945
            case 'D':
950
            case 'D':
946
             printf("1101");
951
             printf("1101");
947
             break;
952
             break;
948
            case 'e':
953
            case 'e':
949
            case 'E':
954
            case 'E':
950
             printf("1110");
955
             printf("1110");
951
             break;
956
             break;
952
            case 'f':
957
            case 'f':
953
            case 'F':
958
            case 'F':
954
             printf("1111");
959
             printf("1111");
955
             break;
960
             break;
956
            default:
961
            default:
957
       printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
962
       printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
958
             break;
963
             break;
959
           }
964
           }
960
          }
965
          }
961
}
966
}
962
 
967
 
963
void hd()
968
void hd()
964
{
969
{
965
         int i,a[20];
970
         int i,a[20];
966
         unsigned long int h=0,m=1;
971
         unsigned long int h=0,m=1;
967
         char s[20];
972
         char s[20];
968
         clrscr();
973
         clrscr();
969
         printf("Convertion from Hexadecimal to Decimal\n");
974
         printf("Convertion from Hexadecimal to Decimal\n");
970
         printf("Enter a Hexadecimal number\n");
975
         printf("Enter a Hexadecimal number\n");
971
         scanf("%s",s);
976
         scanf("%s",s);
972
         printf("Decimal Equivalent=");
977
         printf("Decimal Equivalent=");
973
         for(i=0;s[i]!=NULL;i++)
978
         for(i=0;s[i]!=NULL;i++)
974
         {
979
         {
975
          switch(s[i])
980
          switch(s[i])
976
           {
981
           {
977
            case '0':
982
            case '0':
978
             a[i]=0;
983
             a[i]=0;
979
             break;
984
             break;
980
            case '1':
985
            case '1':
981
             a[i]=1;
986
             a[i]=1;
982
             break;
987
             break;
983
            case '2':
988
            case '2':
984
             a[i]=2;
989
             a[i]=2;
985
             break;
990
             break;
986
            case '3':
991
            case '3':
987
             a[i]=3;
992
             a[i]=3;
988
             break;
993
             break;
989
            case '4':
994
            case '4':
990
             a[i]=4;
995
             a[i]=4;
991
             break;
996
             break;
992
            case '5':
997
            case '5':
993
             a[i]=5;
998
             a[i]=5;
994
             break;
999
             break;
995
            case '6':
1000
            case '6':
996
             a[i]=6;
1001
             a[i]=6;
997
             break;
1002
             break;
998
            case '7':
1003
            case '7':
999
             a[i]=7;
1004
             a[i]=7;
1000
             break;
1005
             break;
1001
            case '8':
1006
            case '8':
1002
             a[i]=8;
1007
             a[i]=8;
1003
             break;
1008
             break;
1004
            case '9':
1009
            case '9':
1005
             a[i]=9;
1010
             a[i]=9;
1006
             break;
1011
             break;
1007
            case 'a':
1012
            case 'a':
1008
            case 'A':
1013
            case 'A':
1009
             a[i]=10;
1014
             a[i]=10;
1010
             break;
1015
             break;
1011
            case 'b':
1016
            case 'b':
1012
            case 'B':
1017
            case 'B':
1013
             a[i]=11;
1018
             a[i]=11;
1014
             break;
1019
             break;
1015
            case 'c':
1020
            case 'c':
1016
            case 'C':
1021
            case 'C':
1017
             a[i]=12;
1022
             a[i]=12;
1018
             break;
1023
             break;
1019
            case 'd':
1024
            case 'd':
1020
            case 'D':
1025
            case 'D':
1021
             a[i]=13;
1026
             a[i]=13;
1022
             break;
1027
             break;
1023
            case 'e':
1028
            case 'e':
1024
            case 'E':
1029
            case 'E':
1025
             a[i]=14;
1030
             a[i]=14;
1026
             break;
1031
             break;
1027
            case 'f':
1032
            case 'f':
1028
            case 'F':
1033
            case 'F':
1029
             a[i]=15;
1034
             a[i]=15;
1030
             break;
1035
             break;
1031
            default:
1036
            default:
1032
    printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
1037
    printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
1033
             break;
1038
             break;
1034
           }
1039
           }
1035
         }
1040
         }
1036
         i--;
1041
         i--;
1037
         for(;i>=0;i--)
1042
         for(;i>=0;i--)
1038
         {
1043
         {
1039
          h=h+a[i]*m;
1044
          h=h+a[i]*m;
1040
          m=m*16;
1045
          m=m*16;
1041
         }
1046
         }
1042
          printf("%ld ",h);
1047
          printf("%ld ",h);
1043
}
1048
}
1044
 
1049
 
1045
void oh(long n)
1050
void oh(long n)
1046
{
1051
{
1047
         long i;
1052
         long i;
1048
         if(n>0)
1053
         if(n>0)
1049
         {
1054
         {
1050
          i=n%16;
1055
          i=n%16;
1051
          n=n/16;
1056
          n=n/16;
1052
          oh(n);
1057
          oh(n);
1053
          if(i>=10)
1058
          if(i>=10)
1054
          {
1059
          {
1055
           switch(i)
1060
           switch(i)
1056
           {
1061
           {
1057
            case 10:
1062
            case 10:
1058
             printf("A");
1063
             printf("A");
1059
             break;
1064
             break;
1060
            case 11:
1065
            case 11:
1061
             printf("B");
1066
             printf("B");
1062
             break;
1067
             break;
1063
            case 12:
1068
            case 12:
1064
             printf("C");
1069
             printf("C");
1065
             break;
1070
             break;
1066
            case 13:
1071
            case 13:
1067
             printf("D");
1072
             printf("D");
1068
             break;
1073
             break;
1069
            case 14:
1074
            case 14:
1070
             printf("E");
1075
             printf("E");
1071
             break;
1076
             break;
1072
            case 15:
1077
            case 15:
1073
             printf("F");
1078
             printf("F");
1074
             break;
1079
             break;
1075
           }
1080
           }
1076
          }
1081
          }
1077
          else
1082
          else
1078
           printf("%ld",i);
1083
           printf("%ld",i);
1079
         }
1084
         }
1080
}
1085
}
1081
 
1086
 
1082
void ho()
1087
void ho()
1083
{
1088
{
1084
         int i,a[20];
1089
         int i,a[20];
1085
         unsigned long int h=0,m=1;
1090
         unsigned long int h=0,m=1;
1086
         char s[20];
1091
         char s[20];
1087
         clrscr();
1092
         clrscr();
1088
         printf("Convertion from Hexadecimal to Octal\n");
1093
         printf("Convertion from Hexadecimal to Octal\n");
1089
         printf("Enter a Hexadecimal number\n");
1094
         printf("Enter a Hexadecimal number\n");
1090
         scanf("%s",s);
1095
         scanf("%s",s);
1091
         // Converting hex to dec first
1096
         // Converting hex to dec first
1092
         for(i=0;s[i]!=NULL;i++)
1097
         for(i=0;s[i]!=NULL;i++)
1093
         {
1098
         {
1094
          switch(s[i])
1099
          switch(s[i])
1095
           {
1100
           {
1096
            case '0':
1101
            case '0':
1097
             a[i]=0;
1102
             a[i]=0;
1098
             break;
1103
             break;
1099
            case '1':
1104
            case '1':
1100
             a[i]=1;
1105
             a[i]=1;
1101
             break;
1106
             break;
1102
            case '2':
1107
            case '2':
1103
             a[i]=2;
1108
             a[i]=2;
1104
             break;
1109
             break;
1105
            case '3':
1110
            case '3':
1106
             a[i]=3;
1111
             a[i]=3;
1107
             break;
1112
             break;
1108
            case '4':
1113
            case '4':
1109
             a[i]=4;
1114
             a[i]=4;
1110
             break;
1115
             break;
1111
            case '5':
1116
            case '5':
1112
             a[i]=5;
1117
             a[i]=5;
1113
             break;
1118
             break;
1114
            case '6':
1119
            case '6':
1115
             a[i]=6;
1120
             a[i]=6;
1116
             break;
1121
             break;
1117
            case '7':
1122
            case '7':
1118
             a[i]=7;
1123
             a[i]=7;
1119
             break;
1124
             break;
1120
            case '8':
1125
            case '8':
1121
             a[i]=8;
1126
             a[i]=8;
1122
             break;
1127
             break;
1123
            case '9':
1128
            case '9':
1124
             a[i]=9;
1129
             a[i]=9;
1125
             break;
1130
             break;
1126
            case 'a':
1131
            case 'a':
1127
            case 'A':
1132
            case 'A':
1128
             a[i]=10;
1133
             a[i]=10;
1129
             break;
1134
             break;
1130
            case 'b':
1135
            case 'b':
1131
            case 'B':
1136
            case 'B':
1132
             a[i]=11;
1137
             a[i]=11;
1133
             break;
1138
             break;
1134
            case 'c':
1139
            case 'c':
1135
            case 'C':
1140
            case 'C':
1136
             a[i]=12;
1141
             a[i]=12;
1137
             break;
1142
             break;
1138
            case 'd':
1143
            case 'd':
1139
            case 'D':
1144
            case 'D':
1140
             a[i]=13;
1145
             a[i]=13;
1141
             break;
1146
             break;
1142
            case 'e':
1147
            case 'e':
1143
            case 'E':
1148
            case 'E':
1144
             a[i]=14;
1149
             a[i]=14;
1145
             break;
1150
             break;
1146
            case 'f':
1151
            case 'f':
1147
            case 'F':
1152
            case 'F':
1148
             a[i]=15;
1153
             a[i]=15;
1149
             break;
1154
             break;
1150
            default:
1155
            default:
1151
      printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
1156
      printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
1152
             break;
1157
             break;
1153
           }
1158
           }
1154
         }
1159
         }
1155
         i--;
1160
         i--;
1156
         for(;i>=0;i--)
1161
         for(;i>=0;i--)
1157
         {
1162
         {
1158
          h=h+a[i]*m;
1163
          h=h+a[i]*m;
1159
          m=m*16;
1164
          m=m*16;
1160
         }
1165
         }
1161
         // Now convering from decimal to octal (h)
1166
         // Now convering from decimal to octal (h)
1162
         i=0;
1167
         i=0;
1163
         printf("Octal equavalent=");
1168
         printf("Octal equavalent=");
1164
         while(h!=0)
1169
         while(h!=0)
1165
         {
1170
         {
1166
          a[i]=h%8;
1171
          a[i]=h%8;
1167
          h=h/8;
1172
          h=h/8;
1168
          i++;
1173
          i++;
1169
         }
1174
         }
1170
         i--;
1175
         i--;
1171
         for(;i>=0;i--)
1176
         for(;i>=0;i--)
1172
          printf("%d",a[i]);
1177
          printf("%d",a[i]);
1173
}*/
1178
}*/
1174
 
1179