# Pointer array's and ASCII to bit conversion

This is a discussion on Pointer array's and ASCII to bit conversion within the C++ Programming forums, part of the General Programming Boards category; I need to know some information: 1) In my code, i use a double for loop to run through a ...

1. ## 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. 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. 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)

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. 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. 1) I would guess:
Code:
`pointer[x][y].variable_to_modify = (variable amount)`
but I'd have to see the declaration.

7. Wow. That makes things easy, swoopy.

One note: the references online say CHAR_BIT is defined in <climits>

8. >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. VC++6 let me use <limits> too, so I assumed <bitset> must include <climits>, but you're probably right.

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

11. 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.

12. Thanks a lot I really appreciate this help

13. *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.