13 Replies - 2195 Views - Last Post: 10 January 2012 - 06:59 AM Rate Topic: -----

#1 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 05:17 PM

I need some help for this text-based calculator that I am making. I'm having a problem with coverting a double to a char array. I tried to convert the double to a string then using strcpy() to input the string into a array but I'm getting an error "cannot convert 'std::string to 'char*' for argument" which makes since so is there another function that I can use for this or am I going down the wrong path about this?
this is my code so far.
void Bin_to_Base10() {

std::ostringstream os;
os << number1;
std::string str = os.str();
   l = binary.length();
   strcpy (binary,number1);
   for (int i=l; x<0; x--) {
     if (binary[i]==1) {
        x=x+c;
        c=c*2;}


    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Binary to Base 10 converter double to string or char array problem?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,890
  • Joined: 25-December 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 05:27 PM

The following: os << number1; converts the number into a ostringstream. The following line std::string str = os.str(); converts this ostringstream into a string. What type of variable is binary? You do not use the C-string functions with std::strings. If binary is a std::string you can use the assignment operator=.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 LordVicus  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 57
  • Joined: 05-November 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 05:33 PM

I may be missing something here, but your for loop declares an int variable "i" and initializes it to one. YOu are using this as an index value but it is never iterated. I am assuming many of your variables are global since they are neither declared nor passed to this function. In that case, what type is the variable "binary", and why are you calling its length() method to assign its value to a literal?
Was This Post Helpful? 0
  • +
  • -

#4 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 08:28 PM

Binary is my string array. I am trying to get Number1 which is a double into that array so I can loop through each of the single digits to look for a 1 or a 0 and go from there.
Sorry if I'm not being specific. I done some very shallow research on trying to find the length of an array and I found .length so I don't know if that is the proper usage or not.
I am completely new to C++ so go easy on me please.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,890
  • Joined: 25-December 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 08:36 PM

Please show some samples of what you want. Show us what number1 contains, what you think your variable str contains, and what you want your output to be.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 08:42 PM

View Postjimblumberg, on 09 January 2012 - 08:36 PM, said:

Please show some samples of what you want. Show us what number1 contains, what you think your variable str contains, and what you want your output to be.

Jim

Number1 will contain a binary number such as 101, I think my variable str contains the string equivalent to number1 ("101"), I would like my output to be the base 10 equivalent to the base 2 "101" which is 5.
Hopefully I'm being specific enough now.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,890
  • Joined: 25-December 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 08:51 PM

Quote

Number1 will contain a binary number such as 101, I think my variable str contains the string equivalent to number1 ("101")

Yes str will contain the string equivalent to number1, but not the binary equivalent. If number1 is equal to 15, str will contain the characters '1', '5', and the end of string character ('\0'). There is no standard function that converts a number to a binary string ("0101"). You may find this link useful: Binary Numbers. Also this link: How to convert from Decimal to Binary

Jim

This post has been edited by jimblumberg: 09 January 2012 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 09:13 PM

View Postjimblumberg, on 09 January 2012 - 08:51 PM, said:

Quote

Number1 will contain a binary number such as 101, I think my variable str contains the string equivalent to number1 ("101")

Yes str will contain the string equivalent to number1, but not the binary equivalent. If number1 is equal to 15, str will contain the characters '1', '5', and the end of string character ('\0'). There is no standard function that converts a number to a binary string ("0101"). You may find this link useful: Binary Numbers. Also this link: How to convert from Decimal to Binary

Jim

I know that there's not a standard function for that, thats why I'm attempting to make it. I've actually made it before in Visual Basic but that is less strict and totally different. Thanks for the links though.. How can I put str into an array, that is my biggest problem here I think. If I can get it into an array I can search through it character by character with a for next loop.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,890
  • Joined: 25-December 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 09:25 PM

What do you mean by putting str into an array? A std::string is basically an array of characters. You can search a string the same way you search an array using array notation str[].

Jim
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 09:30 PM

View Postdylan1020, on 09 January 2012 - 10:42 PM, said:

Number1 will contain a binary number such as 101, I think my variable str contains the string equivalent to number1 ("101"), I would like my output to be the base 10 equivalent to the base 2 "101" which is 5.
Hopefully I'm being specific enough now.

You really do seem to be flailing around in the dark. I don't mean that as a criticism -- just recognizing that you're a bit lost. Here are a few things that you need to be aware of. Maybe you already are aware of some or all of this, but it's not apparent from what you wrote.

First, regarding your statement that "Number1 will contain a binary number such as 101": although all numbers are represented as binary numbers in the computer, thats "behind the scenes" and there's no practical way for you to access numerical data in binary format, so the only practical way to represent a binary number in a form that "looks like" a binary number is as some kind of string -- either a std::string or a char array. And therefore any "binary number" for all practical purposes is some kind of string. So converting a number from binary to decimal means translating a string of '0' and '1' characters into a number -- probably an int.

Second, a std::string is a C++ string -- an object of the C++ string class. A "cstring" is just an array of chars with a "null char" '\0' tacked on to mark the end. You can use either of these for your project, and you can either use C++ string functions or you can use functions like strcpy from the C string library <cstring>. But you really shouldn't use both unless you have a really good reason to do so, which you don't have in this assignment, and if you're studying C++ you should give preference to C++ strings.

So decide which kind of strings you want to use and stick to them. You can refer to either C++ string class or C strings as well as the binary number links that Jim posted and see if you can make some progress.
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,563
  • Joined: 09-June 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 09 January 2012 - 09:36 PM

As far as logic goes, i'm not sure what you are attempting to do. Binary is base 2 and decimal is base 10, it's not hard to convert: example

Multiple each binary value by a value of two to the power of some increasing value- since it's base 2 - and add together all values.

Example :

1110 = (0 * 2^0) + (1 * 2^1) + (1 * 2^2) + (1 * 2^3) = 14

example in C++

int binToBase10(const std::string bin) {
	int base10 = 0;
	int power = 0;
	for(int i=bin.length()-1; i>=0; i--) {
              //input what you think goes in here

	}
	return base10;
}


This post has been edited by jjl: 09 January 2012 - 09:44 PM

Was This Post Helpful? 0
  • +
  • -

#12 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Re: Binary to Base 10 converter double to string or char array problem?

Posted 10 January 2012 - 06:29 AM

Thank you guys I think I got it now.
@R.Stiltskin,I do know that a binary number is a representation on the screen, not a actual real number. Thank you for the links and info on C++ strings and cstrings.
@Jimblumberg, thank you I did not know that you could search through a string like an array. That leads me to another question, when you declare a string variable and input something into it, you can search through that like an array correct?
@jjl, I understand how you find the base 10 equivalent to binary like that, thank you for the code but I prefer to make my own code and methods for things like this, mine is not that different from yours although it doesn't deal with powers.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4140
  • View blog
  • Posts: 12,890
  • Joined: 25-December 09

Re: Binary to Base 10 converter double to string or char array problem?

Posted 10 January 2012 - 06:40 AM

Quote

hat leads me to another question, when you declare a string variable and input something into it, you can search through that like an array correct?

Yes, plus you also have the benefit of many of the std::string member functions. A std::string is basically an array of characters.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 dylan1020  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 09-January 12

Re: Binary to Base 10 converter double to string or char array problem?

Posted 10 January 2012 - 06:59 AM

View Postjimblumberg, on 10 January 2012 - 06:40 AM, said:

Quote

hat leads me to another question, when you declare a string variable and input something into it, you can search through that like an array correct?

Yes, plus you also have the benefit of many of the std::string member functions. A std::string is basically an array of characters.

Jim

Thank you for all the help you gave me and thank you to everybody else who helped me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1