Subversion Repositories HomeAutomation

Rev

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