someone showed me their code for their conversion program
and left before I could ask question about how his program works
q1:how exactly does "*" this work when not applied for mutipling?
q2:does "a_base" and "b_base" work with this function unsigned long base2dec(char str[],int base) and does it have to do it the "_"?
q3:what is the purpose of the [MAX_BIT]?
q4:what is he trying to do in line 41. to 52.
q5:how do I highlight code on daniweb? ... tried to do it but the preview looked a bit off

1.   #include<iostream>
2.   #include<cmath>
3.   using namespace std;
4.   const char digits[16]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
5.   const int MAX_BIT =32;
6.   int digit2value(char);
7.   char value2digit(int);
8.
9.   unsigned long base2dec(char[],int);
10. char * dec2base(unsigned long, int);
11.
12.  void main(){
13.
14.	char a_num[MAX_BIT],*b_num;
15.	
16.	int a_base,b_base;
17.	
18.	unsigned long dec_num;
19.	
20.		cout<<"enter radix a: ";
21.	cin>>a_base;
22.
23.		cout<<"enter numbers to convert from radix a: ";
24.	cin>>a_num;
25.
26.		cout<<"enter radix b: ";
27.	cin>>b_base;
28.
29.  dec_num = base2dec(a_num,a_base);
30.
31.  b_num = dec2base(dec_num,b_base);
32.
33.		cout<<"converted #s: ";
34.
35.	for(int i= strlen(b_num)-1;i>=0;i--)
36.		cout<<b_num[i];
37.
38.	cout<<endl;
39.  
40.  }
41.  char value2digit(int value){
42. 	
43.  	return digits[value];
44.  }
45 . int digit2value(char digit){
46.	
47.	for(int i=0;i<strlen(digits);i++)
48.	
49.		if(digit==digits[i])
50.
51.			return i;
52.  }
53.
54 . unsigned long base2dec(char str[],int base){
55.	
56.	int ans =0;
57.	
58.		for(int i=strlen(str)-1;i<strlen(str);i--)
59.
60.	ans+=digit2value(str[i])*pow((double)base,(double) strlen(str) -i-1);
61.  return ans;
62.  }
63.
64.  char*dec2base(unsigned long dec, int base){
65.  char * ans = new char[MAX_BIT];
66.  int i=0;
67.
68.  while(dec>0){
69.  ans[i]=value2digit(dec%base);
70.  dec= dec/base;
71.
72.  i++;
73.  }
74.  ans[i] =0;
75.  return ans;
76.  }

Recommended Answers

All 2 Replies

1) It's generally used for declaring and dereferencing pointers.

2) a_base works with base2dec and b_base works with dec2base. You can name them whatever you want as long as they're valid identifiers.

3) The longest number would be in base 2 (binary), so it makes sense to set the size of the output array to the number of bits used to represent your value.

4) In value2digit, he's looking up the character representation of the digit he wants to convert. For example, if the hexadecimal value of the digit is 12, looking up digits[12] give you 'c'. In digit2value, he's doing the opposite: searching for the digit character and using the index as the digit value. For example looking up 'c' in digits stops at index 12, which is the correct value.

5) Add the language you want the code to be highlighted for:

[code=cplusplus]
<your code here>
[/code]

thank you very much for clarify those points

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.