Thread: Coding Binary

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably want a short or an unsigned short then.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    I guess the next question is what is the proper way to convert from a string to an int.
    Example:
    I enter this random number: 0001010101100111
    It should output this:
    S: 0001010101100111
    1: 1110101010011000
    2: 0001010101101000

    This is what I get:
    S: 1001111001001111
    1: 0110000110110000
    2: 1001111001010000

    I am using the atoi function to convert a string to an int. What should I do?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well your S isn't looking like what you expect, unless you messed up your example text. You probably want to use something like strtol instead of atoi.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mightymouse3062 View Post
    I guess the next question is what is the proper way to convert from a string to an int.
    If you mean you literally wanted to transform the string

    0001010101100111

    into an int as if that were the decimal number, a hundred trillion and one or whatever, you are out of luck. It is too big.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Even if I do something less like 13:
    S: 0000010001001101
    1: 1111101110110010
    2: 0000010001001110

    When it should be:
    S: 0000000000001101
    1: 1111111111110010
    2: 0000000000001110

    I am using the atoi function.

    Thanks,
    Mike

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure what "S" is representing here. Is that your input string? What sort of input are you providing? Remember, atoi is going to give you an int, wich is 32bits on your system. So you're going to need to shift some of that away. Also keep in mind shifting signed vs. unsigned is probably different.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM