string to int array

hello mates...

I am a new in C++ and i try to build a project for my school

i have a question.....

in my project i would like to choose a binary number (initialized as Ansistring)
from a listbox(vcl) or something similar convert it to string and then to an int array with each element of the array to be one binary digit in order to use this array into a class i have......

i tried some methods i found in web but i could only asign the whole string into one element of the array....

i use Borland c++ 5

thanks in advance
If I understand this correctly, you want to transform an ansii-character into its binary-digits. These are stored in an int-array.

I do not know if there's a funvtion for that but if not, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13

char ch = 'A'; // just for simplicity.

int ch_value = int(ch) // should return 65.

int ch_binary[8]; // for the 8 digits

for (int i=7;i>=0;i--)
{
     ch_binary[i]=ch_value/int(pow(2,i)) //returns the bit value
     ch_value = ch_value%int(pow(2,i)) //returns the rest 
    // int(pow()) ensures the use of integer-division
}
Last edited on
thanks for the answer.but this is not what i want.....

imagine in a listbox from vcl
i have multiple coises which are trains of binary or decimal int numbers i.e
100001
011001
013501
.
.
.
.
etc

these number trains represent the coeficients of polynomials

as far as i know these coises are in AnsiString format

by choosing one of these options and use a button for example...i would like the above choise to be converted to an integer array with each number to be one element of the int array in order to make some calculations.

I solved it using istringstream......thanks anyway
Last edited on
Topic archived. No new replies allowed.