Subversion Repositories HomeAutomation

Rev

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

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