# Pointer array's and ASCII to bit conversion

• 05-05-2005
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.
• 05-05-2005
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.
• 05-05-2005
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)
• 05-05-2005
7stud
Quote:

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.
• 05-05-2005
swoopy
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; }```
• 05-05-2005
swoopy
1) I would guess:
Code:

`pointer[x][y].variable_to_modify = (variable amount)`
but I'd have to see the declaration.
• 05-05-2005
7stud
Wow. That makes things easy, swoopy. :)

One note: the references online say CHAR_BIT is defined in <climits>
• 05-05-2005
swoopy
>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?
• 05-05-2005
7stud
VC++6 let me use <limits> too, so I assumed <bitset> must include <climits>, but you're probably right.
• 05-05-2005
Dave_Sinkula
Quote:

Originally Posted by swoopy
The question is: where's a good reference for these C++ limits?

• 05-05-2005
7stud
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:
Quote:

Properties described in <limits> include accuracy, minimum and maximum sized representations, rounding, and signaling type errors.
• 05-06-2005
```*(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.```