Decimal >> hexdecimal

I need assistance converting a decimal number into a hexdecimal. I've .txt file with data :

2 3 // these numbers are used for outputting the results (2 collumns 3 rows)
0 128 0
255 0 0
255 255 255
255 255 0
255 0 0
255 255 0

and I need to output :

008000;FF0000;FFFFFF
FFFF00;FF0000;FFFF00

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
  void skaityti_Faila(int& a, int& b, int RGB[100][100])
{
    char laikinas[100];
    int g = 0;

    ifstream in("duomenys.txt");

    in >> a >> b;

    for(int i = 0; i < 6; i++)
        for(int j = 0; j < 3; j++)
            in >> RGB[i][j];

    for(int i = 0; i < 6; i++)
        for(int j = 0; j < 3; j++)
            while(RGB[i][j] != 0)
            {
                int temp = 0;
                temp = RGB[i][j] % 16;
                if(temp < 10)
                {
                    laikinas[g] = temp + 48;
                    g++;
                }
                else
                {
                    laikinas[g] = temp + 55;
                    g++;
                }
                RGB[i][j] = RGB[i][j]/16;
            }
}
Last edited on
@lastchance For this task, we're not allowed to use anything other than % and / to get to our goal.
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;


string base( unsigned n, unsigned b )
{
   return ( n < b ? "" : base( n / b, b ) ) + "0123456789ABCDEF"[ n % b ];
}


int main()
{
   istringstream in( "2 3         \n"
                     "0 128 0     \n"
                     "255 0 0     \n"
                     "255 255 255 \n"
                     "255 255 0   \n"
                     "255 0 0     \n"
                     "255 255 0   \n" );

   ostream &out = cout;
   int rows, cols;
   int R, G, B;

   in >> rows >> cols;
   out << setfill( '0' );
   for ( int r = 0; r < rows; r++ )
   {
      for ( int c = 0; c < cols; c++ )
      {
         in >> R >> G >> B;
         out << setw( 2 ) << base( R, 16 ) << setw( 2 ) << base( G, 16 ) << setw( 2 ) << base( B, 16 ) << ';';
      }
      out << '\n';
   }
}


008000;FF0000;FFFFFF;
FFFF00;FF0000;FFFF00;
Last edited on
I've few questions, since I'm not familiar with some of the parts in this code. So maybe you could explain them to me.

1
2
3
4
string base( unsigned n, unsigned b )
{
   return ( n < b ? "" : base( n / b, b ) ) + "0123456789ABCDEF"[ n % b ];
}


 
out << setfill( '0' );


 
out << setw( 2 ) << base( R, 16 ) << setw( 2 ) << base( G, 16 ) << setw( 2 ) << base( B, 16 ) << ';';
( n < b ? "" : base( n / b, b ) )
If n is less than b then this part is just "" (empty string)
otherwise it recursively moves on to n / b (remaining hexadecimal digits to the left).


+ "0123456789ABCDEF"[ n % b ];
n%b is the index of the last digit in base b (here, 16). Then use this index of the c-string "0123456789ABCDEF" to get this last hexadecimal digit.



out << setfill( '0' );
Fills any space in front with character '0', in case you want to write a 1-digit number into a 2-digit field, and need the 0 characters in front.


out <<
I've used out so that you could easily direct this stream to where you want to (e.g. file). At the moment it is a reference to standard out (cout).


<< setw( 2 )
Write the next item (only) to a field of minimum width 2.


I think the rest of that line is standard c++ output.
Topic archived. No new replies allowed.