Thread: Converting a string to an integer

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22

    Converting a string to an integer

    So I need to write some code to take in a 6 bit binary "integer" that is actually a string of characters and I need to convert it into in an binary integer so that I can perform further functions on it. I know that I need to subtract the value by 48 I know that, but I'm not really sure as to how I would go about about doing that. I know that I should have code but I know there isn't much and I am just kinda lost. I think that I need to take it in then subtract the value and put out a new value but again fairly lost. Any help could be appreciated, I cant wrap my head around this one.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Quote Originally Posted by eater View Post
    So I need to write some code to take in a 6 bit binary "integer" that is actually a string of characters and I need to convert it into in an binary integer so that I can perform further functions on it. I know that I need to subtract the value by 48 I know that, but I'm not really sure as to how I would go about about doing that. I know that I should have code but I know there isn't much and I am just kinda lost. I think that I need to take it in then subtract the value and put out a new value but again fairly lost. Any help could be appreciated, I cant wrap my head around this one.
    The word binary integer is kind of confusing, because at the low level everything is binary, whether its integer or character or string or float.
    Coming to the solution you are looking for, you can use this function,
    #include <stdlib.h>
    i = atoi(s);
    the integer will be stored in 'i'.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by sbattu View Post
    The word binary integer is kind of confusing, because at the low level everything is binary, whether its integer or character or string or float.
    Coming to the solution you are looking for, you can use this function,
    #include <stdlib.h>
    i = atoi(s);
    the integer will be stored in 'i'.
    It sounds like he is going to be entering in actual characters so atoi wont do what he needs

    in order to convert a number into another base you can put this into a loop until the number is 0
    Code:
     
    holddigits[index] = number % BASE;
    number /= BASE;
    It will get you the least significant digit first so you will need to output it in reverse order
    Last edited by ಠ_ಠ; 04-25-2009 at 11:34 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Ok so I don't think I was as clear as I would have liked. Thanks for the help though. I think the point was that we don't use the atoi function because I thought of that as well. Here is the actual description from the assignment. Yes it is part of hw but again its simply a small part in a large program of which I have written most of the functioning code for already however I am taking the input in as an integer rather than a character. we are supposed to take in a 6-bit binary sequence (i.e '100110') and its supposed to convert it to an integer before we do anything. an integer in binary that represents the exact same thing to us (i.e. '100110') however of course to the computer who reads characters and inteers differently it will read the second as binary and the first as (49,48,48,49,49,48). so the question is how do i convert this? I have posted the part of the prompt which explains this here. I'm not trying to cheat my way through this. it really is a small part of the entire scheme of the program I'm just getting tripped up on it and would like some help.

    "Accept the binary representation of the integer to be converted to octal representation as a string. You can assume that the binary representation consists of 6 bits.

    Since the integer was read as a string, each bit of the integer would actually be a character. So, before doing any arithmetic on the bits, they must first be converted to integer values. This can be accomplished by subtracting the value 48 from the values of the bits. The reason for doing this is that in the list of ASCII codes for characters, '0' has a value of 48, '1' has a value of 49, and so on. "

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Here is the code that I have been trying to work with. I dunno, this should be really simple because all I want to do is take in a string (100110) and convert the string then to integers that look example the same so I can read them as a binary number (100110).

    #include<stdio.h>

    int Convert(char a[]);

    int main()
    {
    int i = 0;
    char input[6];

    printf("Enter a number: ");
    scanf("%s", &input);

    printf("%d", Convert(input));
    }

    int Convert(char a[])
    {
    int i = 0;
    int z[i];

    for (i = 0; i < 7; i++)
    {
    z[i] == a[i] - 49;
    }
    return z[i];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 10:01 PM