converting data from decimal to octal to binary

Pages: 12
i believe this code correctly converts binary data to octal and then to decimal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  #include <math.h>
#include <stdio.h>

main()
{  
   int octal_array[48] = {0};
   int decimal_array[24] = {0};
   int i;
   int binary_array[144] = //array to convert to 48 octals and 24 dec 
   {
       0,0,0,0,0,0,    // each line has two octals
       0,0,0,0,0,1,
       0,0,0,0,1,0,
       0,0,0,0,1,1,
       0,0,0,1,0,0,
       0,0,0,1,0,1,
       0,0,0,1,1,0,
       0,0,0,1,1,1,
       0,0,1,0,0,0,
       0,0,1,0,0,1,
       0,0,1,0,1,0,
       0,0,1,0,1,1,
       0,0,1,1,0,0,
       0,0,1,1,0,1,
       0,0,1,1,1,0,
       0,0,1,1,1,1,
       0,1,0,0,0,0,
       0,1,0,0,0,1,
       0,1,0,0,1,0,
       0,1,0,0,1,1,
       0,1,0,1,0,0,
       0,1,0,1,0,1,
       0,1,0,1,1,0,
       0,1,0,1,1,1,
   };

   for (i=0; i<144; i++)
   {
      const int idx = i / 3;
      octal_array[idx] <<= 1;
      octal_array[idx] |= binary_array[i];
   }
   for (i=0; i<48; i++) printf( "%d", octal_array[i]);  //print each element of the array
                        printf("\n");
   for (i=0; i<48; i++)
   {
      const int idx = i / 2;
      decimal_array[idx] <<= 3;
      decimal_array[idx] |= octal_array[i];
   }
   for (i=0; i<24; i++) printf( "%d", decimal_array[i]);  //print each element of the array
                        printf("\n");
}

the output is
1
2
000102030405060710111213141516172021222324252627
01234567891011121314151617181920212223


now i need to get back to original 144-bits. is there a way to do this? probably need to get back to octal first. i haven't found a way yet. any help will be super appreciated. Thanks.

looking at the output you have that wouldn't be a correct conversion.

Octal should be something like this in decimal

0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20 etc..


I'm assuming by the way you have it organized you want 24 binary numbers with 6 bits each?
Last edited on
ok, i took a look at this and this is what i got.

There is probably a more "elegant" way to do it but this seems to work fine.

I changed the last two binary value to have the largest bit a 1 to verify the proper formula conversion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <math.h>
#include <stdio.h>

int main()
{
   int octal_array[48];
   int decimal_array[24] = {0};
   int i = 0;
   int oct_iterate = 0; //integer to iterate the octal conversion
   int binary_array[] = //array to initialize 24 binary numbers with 6 bits each
   {
       0,0,0,0,0,0,    // each line is one binary number with 6 bits
       0,0,0,0,0,1,
       0,0,0,0,1,0,
       0,0,0,0,1,1,
       0,0,0,1,0,0,
       0,0,0,1,0,1,
       0,0,0,1,1,0,
       0,0,0,1,1,1,
       0,0,1,0,0,0,
       0,0,1,0,0,1,
       0,0,1,0,1,0,
       0,0,1,0,1,1,
       0,0,1,1,0,0,
       0,0,1,1,0,1,
       0,0,1,1,1,0,
       0,0,1,1,1,1,
       0,1,0,0,0,0,
       0,1,0,0,0,1,
       0,1,0,0,1,0,
       0,1,0,0,1,1,
       0,1,0,1,0,0,
       0,1,0,1,0,1,
       1,1,1,1,1,0, // 62 in binary
       1,1,1,1,1,1, // 63 in binary
   };

   //convert binary data to octal array
   while(i<144)
   {
       octal_array[oct_iterate]   = ((binary_array[i]*4)   + (binary_array[i+1]*2) + binary_array[i+2]);
       octal_array[oct_iterate+1] = ((binary_array[i+3]*4) + (binary_array[i+4]*2) + binary_array[i+5]);
       oct_iterate = oct_iterate + 2;
       i = i + 6; //jump to next binary number
   }
   //print out octal number
   i = 0;
   while(i<48) {std::cout << "Octal Number " << (i/2) << " is : " << octal_array[i] << octal_array[i+1] << " In decimal\n";//print each element of the array
                i = i + 2;}  //jump to next octal number

   //convert from octal to decimal
   oct_iterate = 0;
   for(i=0;i<24;i++){
    decimal_array[i] = ((octal_array[oct_iterate]*8) + octal_array[oct_iterate+1]);
    oct_iterate = oct_iterate + 2;
   }

   //print out decimal number
   for(i=0;i<24;i++){std::cout << "Decimal Number " << i << " is : " << decimal_array[i] << "\n";}

   return 0;
}
Last edited on
Why are you using an array for binary numbers you should use std::bitset

On a side note you can use the stoi function to convert to other bases.
Last edited on
idk, figured he wanted an array.
was more towards the OP since he/she had the array aswell.
Last edited on
how would you use a stoi function?
Well I should have phrased better I mean from any base to base 10.
http://www.cplusplus.com/reference/string/stoi/?kw=stoi

looking at the output you have that wouldn't be a correct conversion.

Octal should be something like this in decimal

0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20 etc..

i am unclear why 10 in octal would not be 8 in dec ?? Thanks!

I'm assuming by the way you have it organized you want 24 binary numbers with 6 bits each?

actually it would be awesome to have a 24 element array containing these values ( this was printed from a 48 element array):
 
000102030405060710111213141516172021222324252627

if i could group these 48 elements into 24 elements that would spare me the conversion to decimal all together.

Why are you using an array for binary numbers you should use std::bitset

that is how i get the data, that array, or the bits in that array are a data payload and that is how they are given to me. so to keep with the convention of program i am trying to work on, i like to use the array to hold the data. in addition to that i don't know how to use the bitset library.

Thanks!!
Last edited on
this little loop here
1
2
3
4
5
6
7
8
for (i=0; i<144; i++)
   {
      const int idx = i / 6;
      dec_array[idx] <<= 1;
      dec_array[idx] |= binary_array[i];
   }
   for (i=0; i<24; i++) printf( "%d", dec_array[i]);  //print each element of the array
                        printf("\n");

will give us the decimal value of each of the 24 6-bit lines in that array:
01234567891011121314151617181920212223

How can we get back to original the 144 bits?

Thanks!
Last edited on
i have not found any easy way to go from dec to binary yet. ok so because the way the data is serialized (i 6-bit groups) there will never be a decimal value over 63. so do i need to do 64 if thens? there has go to be a better way.
 
i am unclear why 10 in octal would not be 8 in dec ?? Thanks!


That's just the way the number system is set up. A mathematician could probably give a better answer than that, but I'm just going with that's how it works. but 10 in octal is 8 in decimal.

Think about decimal for example. It's base 10.

so 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 then it jumps to 10

likewise.. base 8 would be

0, 1, 2, 3, 4, 5, 6, 7, then jump to 10

 
if i could group these 48 elements into 24 elements that would spare me the conversion to decimal all together. 


I think what you are looking for is called concantenation.

I'm still pretty new at this but I might read about that later. I've been meaning to learn how to merge strings / arrays.


Last edited on
if you convert octal number 10 to decimal number you get 8. im not sure how much more to it there is?
that's right. That program i put up there does the conversion also.
but back to the original post. how can we get from a decimal value to a binary value ??
I'd imagine it would be as simple as making a loop and adding a new element to an array after each loop pass.

The loop would simply divide the number by 2 and if there was a remainder it would add a 1 to the array. If no remainder add a 0.

Maybe look like this.


Decimal number 15

15 / 2 = 7 and a remainder so 1 would go to array
7 / 2 = 3 and a remainder so 1 would go to array
3 / 2 = 1 and a remainder so 1 would go to array
1 / 2 = 0 and a remainder so 1 would go to array.

array would be 1111 or 15 in binary.

try with something like 19 now

19/2 = 8 and a remainder so 1 would go to array
8/2 = 4 without a remainder so 0 would go to array
4/2 = 2 without a remainder so 0 would go to array
2/2 = 1 without a remainder so 0 would go to array
1/2 = 0 with a remainder so 1 would go to array.

array would be 10001 or 19 in binary.

You could probably make some kind of conversion loop using that idea of dividing and using remainder to set element. Seems like a simple enough challenge that I might be able to do. But I don't have time at the moment.

I'm sure you can do it though :)


edit.. hmm had a thought

if it was an even number like 12 and you went with that logic above it would come out to 0011.. however if you swapped all the bits it would be correct.

So you'd have to test if the number was even or odd first. if it was odd the above formula would be fine.

if it was even you'd have to do a ! (not) on the result and then it'd be fine.
Last edited on
Converts binary to decimal and then decimal to binary.
(sorry about the mix of printf and cout).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int main()
{
    int decimal_array[24] = {0};

    int binary_array[144] = // array to convert to 24 dec
    {
       0,0,0,0,0,0,
       0,0,0,0,0,1,
       0,0,0,0,1,0,
       0,0,0,0,1,1,
       0,0,0,1,0,0,
       0,0,0,1,0,1,
       0,0,0,1,1,0,
       0,0,0,1,1,1,
       0,0,1,0,0,0,
       0,0,1,0,0,1,
       0,0,1,0,1,0,
       0,0,1,0,1,1,
       0,0,1,1,0,0,
       0,0,1,1,0,1,
       0,0,1,1,1,0,
       0,0,1,1,1,1,
       0,1,0,0,0,0,
       0,1,0,0,0,1,
       0,1,0,0,1,0,
       0,1,0,0,1,1,
       0,1,0,1,0,0,
       0,1,0,1,0,1,
       0,1,0,1,1,0,
       0,1,0,1,1,1,
    };

    // Binary to decimal
    for (int i=0; i<24; i++)
    {
        int temp = 0;

        for (int j = 0; j<6; j++)
            temp |= binary_array[i*6 + j] << (5 - j);

        decimal_array[i] = temp;
    }


    for (int i=0; i<24; i++)
        printf( "%2d ", decimal_array[i]);
    printf("\n");

    // ------------ Now reverse the process --------------------
    // New Array
    int bin_arr[144];

    // convert decimal to binary.
    for (int i=0; i<24; i++)
    {
        for (int j = 0; j<6; j++)
        {
            int mask = 1 << (5 - j);
            bin_arr[i*6 + j] = ((decimal_array[i] & mask) != 0);
        }
    }

    // output the new binary array
    for (int i=0; i<144; i++)
    {
        if (i % 6 == 0)
            cout << endl;
        else
            cout << ", ";
        cout << bin_arr[i];
    }

    return 0;
}

Output:

 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1
0, 0, 0, 0, 1, 0
0, 0, 0, 0, 1, 1
0, 0, 0, 1, 0, 0
0, 0, 0, 1, 0, 1
0, 0, 0, 1, 1, 0
0, 0, 0, 1, 1, 1
0, 0, 1, 0, 0, 0
0, 0, 1, 0, 0, 1
0, 0, 1, 0, 1, 0
0, 0, 1, 0, 1, 1
0, 0, 1, 1, 0, 0
0, 0, 1, 1, 0, 1
0, 0, 1, 1, 1, 0
0, 0, 1, 1, 1, 1
0, 1, 0, 0, 0, 0
0, 1, 0, 0, 0, 1
0, 1, 0, 0, 1, 0
0, 1, 0, 0, 1, 1
0, 1, 0, 1, 0, 0
0, 1, 0, 1, 0, 1
0, 1, 0, 1, 1, 0
0, 1, 0, 1, 1, 1
Last edited on
My $0.02

The "conversion" here is a little misleading. Numbers are numbers. You don't have an octal number or a decimal number... you just have a number. What changes is how that number is displayed through text.

The number ten is always ten... regardless of whether it's represented in text as:
"10" (decimal)
"A" (hexadecimal)
"12" (octal)
"1010" (binary)


With that in mind... I would opt for changing how the number are printed... rather than trying to change the number itself. <iomanip> already has the facilities to do this for you:

1
2
3
4
5
6
7
#include <iostream>
#include <iomanip>

int foo = 10;
cout << oct << foo << endl;  // <- prints 'foo' as octal...  prints "12"
cout << hex << foo << endl;  // <- prints foo as hex... prints "a"
// though there is no 'bin' for binary printing 


But assuming you can't use those, I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void printInBase(std::ostream& strm, int val, int base)
{
    bool negative = val < 0;
    if(negative)
        val = -val;

    std::string text;
    while(val > 0)
    {
        char digit = static_cast<char>(val % base) + '0';
        if(digit >= 10)    digit += 'A' - '0' - 10;

        text.push_back( digit );
        val /= base;
    }
    if(text.empty())
        strm << "0";
    else
    {
        if(negative)  text.push_back('-');
        std::reverse( text.begin(), text.end() );
        strm << text;
    }
}

void printAsOctal (std::ostream& strm, int val) { printInBase(strm,val, 8); }
void printAsHex   (std::ostream& strm, int val) { printInBase(strm,val,16); }
void printAsBinary(std::ostream& strm, int val) { printInBase(strm,val, 2); }
destiny wrote:
i have not found any easy way to go from dec to binary yet.


If you have used the bitwise operators at all you could get it fairly simple (in reverse order)

1
2
3
4
5
6
7
int i = 0;
while( value > 0 )
{
    array[i] = value & 1;
    value >>= 1;
    ++i;
}


or to be safer with memory management use a stl container.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

int main()
{
	const int decimal = 1234;
	std::vector<int> binary;
	
	int i = 0;
	int value = decimal;
	while( value > 0 )
	{
    	    binary.push_back(value & 1);
    	    value >>= 1;
    	    ++i;
	}
	
	
	for( std::vector<int>::reverse_iterator it = binary.rbegin(); it != binary.rend(); ++it )
		std::cout << *it;
		
	return (0);
}

http://ideone.com/a5KiHa

though it's not the most effective doing it this way would be best to use a bitset since an int is 4 bytes and I only used 1 bit from the 4 bytes with each element.
Last edited on

Converts binary to decimal and then decimal to binary.
(sorry about the mix of printf and cout).

No probs! outstanding solution! thanks you so much.


With that in mind... I would opt for changing how the number are printed... rather than trying to change the number itself. <iomanip> already has the facilities to do this for you:

i hear you but i my needs are not printing (printing is for testing so i can lay eyes on what is happening), the point of this is to conveniently package bits for decoding so that i can read data payload from an encoded bit stream. then once the decoding is done, the bits have to go back into a single array for reading because the payload is not in the form of 6-bit dec numbers.


If you have used the bitwise operators at all you could get it fairly simple (in reverse order)

yes i need to study and learn<bitset> and bitwise operations. but this will work for now becuase memory is cheap!

Thanks soo much everybody! You guys are really awesome!
Pages: 12