Subversion Repositories HomeAutomation

Rev

Rev 997 | Rev 1011 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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