Thread: Converting a char string into an integer and then into binary

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    Converting a char string into an integer and then into binary

    How do you convert a string input into a character array, into an integer value.

    I have created a loop in which the value starts at 0, and each iteration adds 10^n * char value (not ASCII, actual value). This works up until about 20 integers, which is the limit of a long long int in C.

    I need to be able to convert an arbitrary amount of chars in a string into integers and then into binary. I have been banging my head against this problem for 2 and a half days.

    Can someone please shed some light on some ideas I may not be thinking of? Is there any way to convert the character string straight into a binary representation of its value??

    Thanks in advance

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Can you use a library for arbitrary-precision (such as The GNU MP Bignum Library) or do you have to implement this functionality from scratch as an assignment?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    What you are asking is how to converte character array of integers into their integer values? Such as 12345 as a character array and then convert it into its integer value?

    Or are you talking about any character array into integers? such as "ABCDEFG"? If so and you dont want to use its ascii value then I dont understand what integer value you are referring to.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    I'll check that out, but the prof said even Unions were not allowed so I doubt it. I thought I had the problem handled. I haven't explained the full assignment here, but basically my algorithm is sound, but in order to implement it I need some sort of variable storage which I can do arithmetic on that can store a very large integer.

    I don't know if this is the wrong way to go about it, but this is one of the most discouraging moments of my brief computer science career. Am I missing something obvious? If so, any guidance in the right direction would be marvelous. At least for the sake of my sanity.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    No a character limited to characters '0' - '9'. The problem isn't the conversion, it's the scale of conversion that needs to be done. Once I reach a certain order of magnitude, there is no pre-defined datatype for me to store the integer in, and thus I cannot turn the integer into binary

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, you need to decide whether you really need to represent a large integral value as an intermediate step, given that you want to work with values that exceed the capacity of long long types. If so, you need to select some representation (say an array of integers) and work out how to unambiguously implement basic operations (addition, subtraction, multiplication, division, modulo, etc) on that representation.

    From there, conversion of "integer values" back to binary is trivial: the binary digits (in reverse order) would be extracted by repeatedly computing the value modulo 2, subtracting that value, and then dividing by 2.

    It would be easier (at least, for you coding it up) to avoid going by some intermediate representation as an "integer". Given an input string (say "1234567") it is fairly easy to go directly to binary: no need for any intermediate representation as an "integer". If the rightmost digit (7 in this case) is odd, the last binary digit is 1. If you keep subtracting 1 (if value is odd) or zero (if value is even) and dividing by 2 (which can be done by hand using a long division technique, and that type of logic can be coded up) you can extract the binary digits one at a time, in reverse order. Reverse the order, and you will find that "1234567" is represented in binary as "100101101011010000111"

    Incidentally, the ASCII value is the actual value of a character (assuming your machine is using an ASCII character set). It is you who interprets a '0' as the digit zero, not the machine.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Grumpy makes a very important point. If the assignment is to take input like
    72636418745281276354727103685721876829840592882608 7364852895
    and print it out in binary, then you don't need the intermediate step of representing it as an "integer" at all. The char array is a good enough representation to get the bits from. You just need to be able to do a little arithmetic on it, in particular, you need to be able to divide it by 2.

    EDIT: Why does this forum add a space after 50 consecutive characters?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by grumpy View Post
    If you keep subtracting 1 (if value is odd) or zero (if value is even) and dividing by 2
    Actually, you won't have to subtract 1 since (integer) division by two will toss it out anyway.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by oogabooga View Post
    Actually, you won't have to subtract 1 since (integer) division by two will toss it out anyway.
    Sure.

    It depends on how division is implemented, actually - rounding up or rounding down are mathematical conventions, not absolute laws of how things should be done. And since one approach for the OP might be to implement division in different ways, I considered it better to spell out what happens .... even if some steps might be done implicitly rather than explicitly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    I don't get your method. Formerly I was taking the whole number, modding it by 2, and dividing it by 2. That worked on the whole integer.

    I don't see how this works going character by character though. Sorry if I am coming off as dense, but I really tried doing it by hand.

    For example: 17.

    7 = 0111

    1 = 1

    10111 != 17

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You have to be a little more clever than that.
    Example
    Code:
    index: 0 1 2
    char : 1 7 3
    First, write a 1 or 0 depending on whether the last digit is odd or even, respectively.
    So we write a 1.
    Then divide the whole string by 2.
    Start at the right-hand side.

    3 / 2 = 1
    Since this is the rightmost digit, we toss out the 0.5

    Next digit:
    7 / 2 = 3
    Since this is NOT the rightmost digit, we can't just throw away the 0.5. Instead, you add 5 to the digit to the right.

    1 / 2 = 0 and add 5 to the digit to the right.

    This gives 0 8 6

    Then start again at the beginning. This time you'll get a 0 bit since the number is even. Then divide by 2 again....

    Keep going until the number is all zeros.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    32
    Oh my god.. That's brilliant....

    What is the intuition behind that?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Avanish Giri
    What is the intuition behind that?
    It is just long division as taught in many elementary/primary schools, combined with what you already know of using division to compute the bits of an integer. Refer to grumpy's post #6.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Also remember that the bits are going to come out in reverse order, so you'll have to store them up and print them backwards.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a string to an integer
    By eater in forum C Programming
    Replies: 4
    Last Post: 04-26-2009, 02:39 PM
  2. Converting Char Array Loop values to integer
    By azamsharp1 in forum C Programming
    Replies: 8
    Last Post: 10-27-2005, 09:13 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. converting a integer to a string
    By LandMonster in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 12:17 PM
  5. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM