# convert

Page 1 of 1

## 11 Replies - 2948 Views - Last Post: 04 December 2012 - 05:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302576&amp;s=3ec8d4a241b60881e76e450498616708&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 d00n

• New D.I.C Head

Reputation: -2
• Posts: 18
• Joined: 03-December 12

# convert

Posted 03 December 2012 - 11:12 AM

```#include <iostream>

using namespace std;

void decimal2Base(int num, int base, char *result, int offset = 0);
void decimal2Base(int num, int base, char * result, int offset)
{
if (base > 36 || base < 2)
result = "NaN";
else
{
int digitVal = num % base;
*result = (digitVal < 10 ? digitVal + '0' : digitVal - 10 + 'A');

if (num >= base)
decimal2Base(num / base, base, result + 1, offset + 1);
else
{
result[1] = '\0';
result -= offset;
//get result's length
int length = 0;
for (char *ptr = result; *ptr; ptr++)
length++;
//copy result to a temporary string
char *temp = new char[length + 1];
for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++)
*tempPtr = *ptr;
//copy back to result in reversed order
for (int i = length - 1; i >= 0; i--)
result[length - 1 - i] = temp[i];
//free memory of the temporary string
delete [] temp;
}
}
}
int main()
{

int num;
int base;
char result[20];

while (true)
{
cout << "Enter number: ";
cin >> num;
if (num < 0)
cout << "Enter base: ";
cin >> base;
if (base > 36 || base < 2)

decimal2Base(num, base, result);
cout << result << endl;

cout << endl;
}

cout << "\nEnd - Press enter to exit . . . ";
cin.sync();
cin.get();
return 0;
}
```

can anyone help in converting this code to c++ in order to understand it clearly because we use c++ only

Is This A Good Question/Topic? 0

## Replies To: convert

### #2 sepp2k

• D.I.C Lover

Reputation: 2619
• Posts: 4,175
• Joined: 21-June 11

## Re: convert

Posted 03 December 2012 - 11:15 AM

d00n, on 03 December 2012 - 07:12 PM, said:

can anyone help in converting this code to c++

Am I missing something? That code is C++.

This post has been edited by sepp2k: 03 December 2012 - 11:16 AM

Was This Post Helpful? 0

### #3 d00n

• New D.I.C Head

Reputation: -2
• Posts: 18
• Joined: 03-December 12

## Re: convert

Posted 03 December 2012 - 11:25 AM

[/quote]That code is C++.[/quote]
actually it is mixed between c and c++ thats why i cant understand some of the statements
Was This Post Helpful? -1

### #4 sepp2k

• D.I.C Lover

Reputation: 2619
• Posts: 4,175
• Joined: 21-June 11

## Re: convert

Posted 03 December 2012 - 11:33 AM

That's a matter of definition. There are few constructs that are legal C, but not legal C++ and those don't appear in your code.

Now you could of course define that any piece of C++ code that is also legal C code, should be considered as "not C++", but by that definition a line like int x = 42; would not be C++. We could then rewrite it to int x(42);, which would be legal in C++, but not in C, but I can't see what purpose that would serve.

I think it would be easier for everybody if you just explained which parts of the code you don't understand.
Was This Post Helpful? 1

### #5 d00n

• New D.I.C Head

Reputation: -2
• Posts: 18
• Joined: 03-December 12

## Re: convert

Posted 03 December 2012 - 11:44 AM

for example line 14
the program is working 100% but what m asking is about is some of the c statements and how if we would like to write it in c++
Was This Post Helpful? -1

### #6 sepp2k

• D.I.C Lover

Reputation: 2619
• Posts: 4,175
• Joined: 21-June 11

## Re: convert

Posted 03 December 2012 - 11:59 AM

In C++ you might use an std::string rather than a char pointer to represent strings. In that case you'd write line 14 result.push_back(digitVal < 10 ? digitVal + '0' : digitVal - 10 + 'A'); (assuming result is passed in as a reference to an empty string). Other than that not much would change about that line.

Personally I'd rather lay the code out on multiple lines like this:

Quote

char digit;
if(digitVal < 10) {
digit = digitVal + '0';
} else {
digit = digitVal - 10 + 'A';
}
result.push_back(digit);

But that has nothing to do with C vs. C++. I just find it more readable like that.
Was This Post Helpful? 0

### #7 BetaWar

• #include "soul.h"

Reputation: 1513
• Posts: 8,290
• Joined: 07-September 06

## Re: convert

Posted 03 December 2012 - 12:25 PM

Pointers are a huge part of C++, most data structures that are in use at the moment can't be done without a form of pointer (whether they be pointers, or references -- both are just memory addresses).

I would suggest you read a tutorial on pointers; here is one on the basics:
http://www.dreaminco...utorial-part-1/
Was This Post Helpful? 0

### #8 d00n

• New D.I.C Head

Reputation: -2
• Posts: 18
• Joined: 03-December 12

## Re: convert

Posted 04 December 2012 - 10:16 AM

what does this statement means
```result[1] = '\0';
```

and what about these loops

```for (char *ptr = result; *ptr; ptr++)
length++;
//copy result to a temporary string
char *temp = new char[length + 1];
for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++)
*tempPtr = *ptr;
//copy back to result in reversed order
for (int i = length - 1; i >= 0; i--)
result[length - 1 - i] = temp[i];
//free memory of the temporary string
delete [] temp;
```

why we used *ptr; in the loop without condition what is that mean is it related to c?
and if there is a clearer way to present the same idea?
Was This Post Helpful? 0

### #9 no2pencil

• Professor Snuggly Pants

Reputation: 6642
• Posts: 30,931
• Joined: 10-May 07

## Re: convert

Posted 04 December 2012 - 12:04 PM

d00n, on 04 December 2012 - 12:16 PM, said:

what does this statement means
```result[1] = '\0';
```

Because result is a char type array, setting element 1 as \0 says that the value isn't null, but it doesn't have a value. It's either referred to as end of array, or NULL constraint? Maybe someone else can clarify it's actual name.
Was This Post Helpful? 0

### #10 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: convert

Posted 04 December 2012 - 12:34 PM

Check out my code snippets.
Was This Post Helpful? 0

### #11 sepp2k

• D.I.C Lover

Reputation: 2619
• Posts: 4,175
• Joined: 21-June 11

## Re: convert

Posted 04 December 2012 - 04:27 PM

no2pencil, on 04 December 2012 - 08:04 PM, said:

Because result is a char type array, setting element 1 as \0 says that the value isn't null, but it doesn't have a value.

That's not really accurate. '\0' == 0 && 0 == NULL and of course all of those are values.

A C-string is a 0-terminated array of char values where "0-terminated" means that the last character in the string must have the value 0. '\0' is simply the integer 0 of type char¹ (like 0l is the integer 0 of type long). This 0-termination is necessary because C-strings don't know their size, so without a terminator string functions wouldn't know where the string ends (without passing the length as another parameter like you do with other kinds of arrays).

¹ Note that this applies to C++ only. In C '\0' (like all other character literals) has type int, not char.
Was This Post Helpful? 2

### #12 Skydiver

• Code herder

Reputation: 6216
• Posts: 21,456
• Joined: 05-May 12

## Re: convert

Posted 04 December 2012 - 05:54 PM

Quote

why we used *ptr; in the loop without condition what is that mean is it related to c?
and if there is a clearer way to present the same idea?

The loop condition is for it to keep looping until *ptr becomes zero (or false). It's a behavior from C that is perfectly legal in C++. If you wanted something clearer, it would be *ptr != 0 or *ptr != '\0'.
Was This Post Helpful? 1

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }