Thread: Pointer array's and ASCII to bit conversion

  1. #1
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56

    Pointer array's and ASCII to bit conversion

    I need to know some information:
    1)
    In my code, i use a double for loop to run through a two dimensional pointer array, setting each slot to point to a new struct. When i set the data in the struct, is this the appropriate syntax?:
    *pointer[x][y].variable_to_modify = (variable amount)
    2)
    I need to know how to take an ASCII character and convert it into its bit equivalent, and then store the bits into an integer array

    I really appreciate any help.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) Try it.

    2) I would say: convert the char to an int, which you can do by assigning it to an int variable, then figure out whether there is a 2^6 th in the number, then whether there is a 2^5th in the number, 2^4th, etc. If the int is bigger than 2^6th, then store a 1 in your array, otherwise store a 0 in your array. Subtract 2^6th from the int if the int is bigger and proceed.

  3. #3
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    Quote Originally Posted by 7stud
    1) Try it.

    2) I would say: convert the char to an int, which you can do by assigning it to an int variable, then figure out whether there is a 2^6 th in the number, then whether there is a 2^5th in the number, 2^4th, etc. If the int is bigger than 2^6th, then store a 1 in your array, otherwise store a 0 in your array. Subtract 2^6th from the int if the int is bigger and proceed.
    How so? with a for loop testing if the letter-number % (modulus) x (x being the power to test for) = 0? (for those that don't know, modulus returns the remainder of an uneven division operation)

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by AdamLAN
    How so? with a for loop testing if the letter-number % (modulus) x (x being the power to test for) = 0? (for those that don't know, modulus returns the remainder of an uneven division operation)
    I pretty much wrote the pseudo code for you in my last post. Take it line by line and write the code for it. If you get stuck, post your code and what problems you are having.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    2) Here's one idea.
    Code:
    #include <iostream>
    #include <bitset>
    #include <limits>
    using namespace std;
    
    int main()
    {
       char ch = 'A';
    
       bitset<CHAR_BIT> bits(ch);
    
       //Print the binary equivalent of char
       cout << bits << endl;
    
       //Access and print each bit individually starting with least significant bit
       for (int i=0; i<bits.size(); i++)
          cout << bits[i] << endl;
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    1) I would guess:
    Code:
    pointer[x][y].variable_to_modify = (variable amount)
    but I'd have to see the declaration.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Wow. That makes things easy, swoopy.

    One note: the references online say CHAR_BIT is defined in <climits>
    Last edited by 7stud; 05-05-2005 at 09:16 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >One note: the references online say CHAR_BIT is defined in <climits>
    Well Borland let me use <limits>. Maybe <limits> includes <climits>. I'll have to try it with Dev-C++ tomorrow.

    I'm glad you mentioned this 7stud, as there should be something defined in <limits> for this. You know, something along the lines of:
    Code:
    std::numeric_limits<char>::width()
    Of course we've seen min and max:
    Code:
    cout << std::numeric_limits<char>::min() << endl;
    cout << std::numeric_limits<char>::max() << endl;
    The question is: where's a good reference for these C++ limits?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    VC++6 let me use <limits> too, so I assumed <bitset> must include <climits>, but you're probably right.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by swoopy
    The question is: where's a good reference for these C++ limits?
    http://dinkumware.com/manuals/reader...h=limits2.html?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There's some information at MSDN. A list of constants defined in limits.h or <climits>, like CHAR_BIT, CHAR_MIN, and CHAR_MAX, can be found here:

    http://msdn.microsoft.com/library/de...cal_limits.asp

    Elsewhere, the <limits> header file is described as a template that queries the system for system dependent implementations rather than C++ requirements. It says:
    Properties described in <limits> include accuracy, minimum and maximum sized representations, rounding, and signaling type errors.
    http://msdn.microsoft.com/library/de...its_header.asp

  12. #12
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    Thanks a lot I really appreciate this help

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    *pointer[x][y].variable_to_modify = (variable amount)

    Just a small piece of advice. When using such code, use parentheses to clarify what is being dereferenced!

    eg. These all mean different things
    Code:
    *(pointer[x][y].variable_to_modify) = (variable amount)
    (*pointer)[x][y].variable_to_modify = (variable amount)
    (*pointer[x])[y].variable_to_modify = (variable amount)
    (*pointer[x][y]).variable_to_modify = (variable amount)
    etc.
    Without the parentheses, it makes it harder to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM