c++: convert binary array to type int

Discussion in 'OT Technology' started by AbortionSurvivor, Oct 16, 2003.

  1. AbortionSurvivor

    AbortionSurvivor Active Member

    Joined:
    Jun 5, 2002
    Messages:
    3,016
    Likes Received:
    0
    Location:
    Nor. Cal
    i need to convert my binary array to type int.

    for example, my_array[0] = '1';
    how would i convert that to an interger 1.

    i ment to put the single quotes around the 1 as the array is of type char.

    thank you guys.
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Type-casting:

    int i;
    i = int(my_array[0]);
     
  3. crotchfruit

    crotchfruit Guest

    i don't think that's what highasbeep wants. doing int(my_array[0]) would give you the ascii value of the '1' character (which is basically not doing anything to the value..)

    for single characters, you should do simple ascii math, for example:

    Code:
    int i = my_array[0] - '0';
    for strings you can use the atoi function:

    Code:
    int i = atoi(my_array[0]);
    here is one atoi reference i found, there are others:
    http://www.qnx.com/developer/docs/momentics621_docs/neutrino/lib_ref/a/atoi.html
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :doh: Crotchfruit is right. I haven't done any real programming in way too long :o
     
  5. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
  6. crotchfruit

    crotchfruit Guest

    i'd also like to add that i am an idiot. the example i gave "atoi(my_array[0])" will fail because my_array[0] is a char, not a string :p
     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :rofl: Gotta love it when it takes three somewhat experienced C++ programmers to solve such an easy problem.
     

Share This Page