Subversion Repositories HomeAutomation

Rev

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

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