convert

Hello,what is different between these programs that one of them oprate true but another not.
I really worked on them but I didn't can fix the first program.
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
#include "stdafx.h"
#include<iostream>;
using namespace std;
void dectohexa(void);
int _tmain(int argc, _TCHAR* argv[])
{


int number;


	cout<<"Enter the decimal number:";
	cin>>number;

	cout<<"DecimaltoHex:\n";
const int arreysize=1000;
	int counter,i=0,remain;
	char printarrey[arreysize],dectohexa[arreysize];


	while(number!=0)
	{
	remain=number%16;
	number=number/16;
	
	printarrey[i]=dectohexa[remain];
	i++;
	}
	i--;
	for(i;i>=0;i--)
	{
		cout<<printarrey[i];
	}


	return 0;
}

void dectohexa(void)
{
const	int arreysize=1000;
	 char dectohexa[arreysize];
dectohexa[0]='0'; 
	dectohexa[1]='1';
	dectohexa[2]='2';
	dectohexa[3]='3';
	dectohexa[4]='4';
	dectohexa[5]='5';
	dectohexa[6]='6';
	dectohexa[7]='7';
	dectohexa[8]='8';
	dectohexa[9]='9';
	dectohexa[10]='A';
	dectohexa[11]='B';
	dectohexa[12]='C';
	dectohexa[13]='D';
	dectohexa[14]='E';
	dectohexa[15]='F';
}










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
#include "stdafx.h"
#include<iostream>;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


int number;


	cout<<"Enter the decimal number:";
	cin>>number;

	cout<<"DecimaltoHex:\n";
const int arreysize=1000;
	int counter,i=0,remain;
	char printarrey[arreysize],dectohexa[arreysize];


	dectohexa[0]='0'; 
	dectohexa[1]='1';
	dectohexa[2]='2';
	dectohexa[3]='3';
	dectohexa[4]='4';
	dectohexa[5]='5';
	dectohexa[6]='6';
	dectohexa[7]='7';
	dectohexa[8]='8';
	dectohexa[9]='9';
	dectohexa[10]='A';
	dectohexa[11]='B';
	dectohexa[12]='C';
	dectohexa[13]='D';
	dectohexa[14]='E';
	dectohexa[15]='F';


	while(number!=0)
	{
	remain=number%16;
	number=number/16;
	
	printarrey[i]=dectohexa[remain];
	i++;
	}
	i--;
	for(i;i>=0;i--)
	{
		cout<<printarrey[i];
	}


	return 0;
}
In the first program you never initialize dectohexa. Your dectohexa function is a problem because when you type "dectohexa" the compiler doesn't know if you're talking about a function or an array. dectohexa() should also take the array as a parameter instead of create it's own array (which is lost when the function ends)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void initiateArray(char arr[], const int SIZE)
{
  for (int i = 0; i < SIZE; i++)
  {
    if (i < 10) arr[i] = i + '0';
    else arr[i] = i - 10 + 'A';
  }
}

int main(void)
{
  const int SIZE = 16;
  char dectohexa[SIZE];
  initiateArray(dectohexa, SIZE);

  for (int i = 0; i < SIZE; i++)
    cout << dectohexa[i] << " ";
  cout << '\n';

  return 0;
}

ok,thank you so much...
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");  // gets a number
  scanf ("%d",&i);
  itoa (i,buffer,10); // prints the number in base 10
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16); // prints the number in base 16 (the hexadecimal base)
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,8); // prints the number in base 8 (the octal base)
  printf ("octal: %s\n",buffer);
  itoa (i,buffer,2); // prints the number in base 2 (the binary base)
  printf ("binary: %s\n",buffer);
  getchar();
  getchar(); // i added an extra getchar because the program was closing
}


Good luck mate.

[edit] I added comments. hope it helps. [/edit]
[edit] http://www.cplusplus.com/reference/cstdlib/itoa/ [/edit]
Last edited on
oh,usandfriends!
I don't understand this code!!!
I'm really beginner!!
Last edited on
LowestOne!
Can you say me what is the problem now?





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
#include "stdafx.h"
#include<iostream>;
using namespace std;
void dectohex(char);
int _tmain(int argc, _TCHAR* argv[])
{


int number;


	cout<<"Enter the decimal number:";
	cin>>number;

	cout<<"DecimaltoHex:\n";
const int arreysize=1000;
	int i,remain;
	char printarrey[arreysize],dectohexa[arreysize];
	 dectohex(dectohexa[i]);


	while(number!=0)
	{
	remain=number%16;
	number=number/16;
	
	printarrey[i]=dectohexa[remain];
	i++;
	}
	i--;
	for(i;i>=0;i--)
	{
		cout<<printarrey[i];
	}


	return 0;
}


void dectohex(char dectohexa[])
{

dectohexa[0]='0'; 
	dectohexa[1]='1';
	dectohexa[2]='2';
	dectohexa[3]='3';
	dectohexa[4]='4';
	dectohexa[5]='5';
	dectohexa[6]='6';
	dectohexa[7]='7';
	dectohexa[8]='8';
	dectohexa[9]='9';
	dectohexa[10]='A';
	dectohexa[11]='B';
	dectohexa[12]='C';
	dectohexa[13]='D';
	dectohexa[14]='E';
	dectohexa[15]='F';
} 

closed account (18hRX9L8)
Fixed it. Look up ^^^^^ .

[edit] No problem! [/edit]
Last edited on
Thank you usandfriends!
This is a tricky error to find. @Line 4 you say the function takes a char, but @line 41 you say it takes a char[]. Change the function prototype (line 4) to be char[] (which I do believe you will need to name:(char name[]))

You know, I have to say, that's a good way to convert dec to hex (a look up table).
Last edited on
LowestOne wrote:
that's a good way to convert dec to hex

The method is good. But it's actually converting binary to hex.

The only time there is a decimal number is the user input, but that is converted to binary by this statement:
 
    cin>>number;


And if that conversion can be used without batting an eyelid, then the corresponding cout statement must also be acceptable:
 
    cout << hex << uppercase << number;


But that small matter aside, here's my version:
1
2
3
4
5
6
7
8
9
10
11
12
13
    const char hextab[17] = "0123456789ABCDEF";

    unsigned int num;
    cout << "Enter a number" << endl;
    cin >> num;

    string hexstr;
    do {
        hexstr.insert(0,hextab[num & 0x0f]);
        num >>= 4;
    } while (num);

    cout << "string hex:  " << hexstr << endl;

Last edited on
LowestOne:
I changed it as you said but it has problem yet and I don't know what to do!

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
#include "stdafx.h"
#include<iostream>
using namespace std;
void dectohex(char dectohexa[]);
int _tmain(int argc, _TCHAR* argv[])
{


int number;


	cout<<"Enter the decimal number:";
	cin>>number;

	cout<<"DecimaltoHex:\n";
const int arreysize=1000;
	int i,remain;
	char printarrey[arreysize],dectohexa[arreysize];
	 dectohex(dectohexa[i]);


	while(number!=0)
	{
	remain=number%16;
	number=number/16;
	
	printarrey[i]=dectohexa[remain];
	i++;
	}
	i--;
	for(i;i>=0;i--)
	{
		cout<<printarrey[i];
	}


	return 0;
}









void dectohex(char dectohexa[])
{

dectohexa[0]='0'; 
	dectohexa[1]='1';
	dectohexa[2]='2';
	dectohexa[3]='3';
	dectohexa[4]='4';
	dectohexa[5]='5';
	dectohexa[6]='6';
	dectohexa[7]='7';
	dectohexa[8]='8';
	dectohexa[9]='9';
	dectohexa[10]='A';
	dectohexa[11]='B';
	dectohexa[12]='C';
	dectohexa[13]='D';
	dectohexa[14]='E';
	dectohexa[15]='F';
} 


Chervil:
why do you say:
it's actually converting binary to hex.
Can you explain more?
Last edited on
There are some computers which can work internally with decimal numbers, an example being the IBM mainframe. However, most of the time, the machines we use can deal directly only with binary numbers. I use the word 'directly' here, as library code can enable other possibilities.

An ordinary integer value in a C++ program is stored internally as a binary number. All calculations such as addition or multiplication are done using binary arithmetic.

So, if we take the starting point to be an int, and the end result is a string of characters containing the hexadecimal representation, it is converting binary to hex.

If you truly wanted to convert from decimal to hex, more than likely for the sake of convenience, you would first convert the decimal number into binary, and then convert the binary number into hexadecimal.

It's easy to overlook the decimal-to-binary and binary-to decimal conversions, as the cin and cout streams use library code which automatically carries out the necessary conversions for us.

Hope this helps.
Last edited on
thank you,chervil.

oh,my friends!please help me!
I really mixed up.

what is the problem now?

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
#include "stdafx.h"
#include<iostream>
using namespace std;
void dectohex( char dectohexa[],const int arreysize);
int _tmain(int argc, _TCHAR* argv[])
{


	int number;


	cout<<"Enter the decimal number:";
	cin>>number;

	cout<<"DecimaltoHex:\n";
	const int arreysize=1000;
	int i=0,remain;
	char printarrey[arreysize],dectohexa[arreysize];
	dectohex(dectohexa,arreysize);


	while(number!=0)
	{
		remain=number%16;
		number=number/16;

		printarrey[i]=dectohexa[remain];
		i++;
	}
	i--;
	for(i;i>=0;i--)
	{
		cout<<printarrey[i];
	}


	return 0;
}





void dectohex(char dectohexa[],const int arreysize=15)
{
	 dectohexa[arreysize] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
}



Last edited on
thank you every body.
I could resolve it finally...
Topic archived. No new replies allowed.