1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2012 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include <gnuradio/trellis/base.h>
24 #include <cmath>
25 #include <cstdio>
26 #include <stdexcept>
27 
28 namespace gr {
29 namespace trellis {
30 
dec2base(unsigned int num,int base,std::vector<int> & s)31 bool dec2base(unsigned int num, int base, std::vector<int>& s)
32 {
33     int l = s.size();
34     unsigned int n = num;
35     for (int i = 0; i < l; i++) {
36         s[l - i - 1] = n % base; // MSB first
37         n /= base;
38     }
39     if (n != 0) {
40         printf("Number %d requires more than %d digits.", num, l);
41         return false;
42     } else
43         return true;
44 }
45 
base2dec(const std::vector<int> & s,int base)46 unsigned int base2dec(const std::vector<int>& s, int base)
47 {
48     int l = s.size();
49     unsigned int num = 0;
50     for (int i = 0; i < l; i++)
51         num = num * base + s[i];
52     return num;
53 }
54 
dec2bases(unsigned int num,const std::vector<int> & bases,std::vector<int> & s)55 bool dec2bases(unsigned int num, const std::vector<int>& bases, std::vector<int>& s)
56 {
57     int l = s.size();
58     unsigned int n = num;
59     for (int i = 0; i < l; i++) {
60         s[l - i - 1] = n % bases[l - i - 1];
61         n /= bases[l - i - 1];
62     }
63     if (n != 0) {
64         printf("Number %d requires more than %d digits.", num, l);
65         return false;
66     } else
67         return true;
68 }
69 
bases2dec(const std::vector<int> & s,const std::vector<int> & bases)70 unsigned int bases2dec(const std::vector<int>& s, const std::vector<int>& bases)
71 {
72     int l = s.size();
73     unsigned int num = 0;
74     for (int i = 0; i < l; i++)
75         num = num * bases[i] + s[i];
76     return num;
77 }
78 
79 } /* namespace trellis */
80 } /* namespace gr */
81