Subversion Repositories HomeAutomation

Rev

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

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