This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

cast a char[] = "" value(s)

hi Forum!

I have declared an char-array, for a simple Data stream buffer of bits:

char data[] = "101101010011111@".

And now just want cast this items to a integer-value. How can i do this in simplest way, without sum the bits bitwise via pointer?

cu
O. Swid

Parents
  • One thing to note is that the LSB of the ASCII code for '0' is 0, and the LSB of the ASCII code for '1' is 1; thus a simple bitwise-AND with 0x01 will convert these particular ASCII codes to their numerical equivalents.

    Thus you can buid the integer by looping the following:

    result = (result<<1) | (string[i]&0x01);
    Provided, of course, that you are absolutely certain that the string can contain only the characters '0' and '1'...

Reply
  • One thing to note is that the LSB of the ASCII code for '0' is 0, and the LSB of the ASCII code for '1' is 1; thus a simple bitwise-AND with 0x01 will convert these particular ASCII codes to their numerical equivalents.

    Thus you can buid the integer by looping the following:

    result = (result<<1) | (string[i]&0x01);
    Provided, of course, that you are absolutely certain that the string can contain only the characters '0' and '1'...

Children