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

  • You have already been given the means to covert an array of binary digit characters in this thread:

    http://www.keil.com/forum/docs/thread4249.asp

    Since you are asking again, one can only assume that the answers given there were inadequate. To be of further assistance, you'll need to tell us what there was about the previous answers that did not suit your requirements.

    "And now just want cast this items to a integer-value."

    Your use of "cast" here in a C forum is quite confusing, since "cast" has a specific meaning and use in the C language that is entirely incompatible with your apparent intentions. Would we better understand you to mean "convert"?

    "...without sum the bits bitwise via pointer?"

    I don't understand this requirement at all. Could you please restate your root requirement in a different way?

  • You cannot "cast" an array of characters (or a C string, or an idiosyncratic string format with an '@' on the end). You can convert strings to integers with various functions, as I've mentioned before.

    The C cast operator and syntax will not avail you.

    (... Flame of Udun. And unless you get this assignment right:
    you... SHALL NOT... PASS!)

  • Ah, I neglected to mention Drew's contributions to the problem here:

    http://www.keil.com/forum/docs/thread4250.asp

    And that again begs the question:

    What are we doing wrong, or what requirements are you not stating properly, that has caused you to reject the feedback given thusfar?

  • You still seem to be under the fundamental misaprehension that you have an array of bits.
    As explained before, you do not!
    http://www.keil.com/forum/docs/thread4249.asp

    Neither the standard 'C' programming language, nor any Keil extension allows you to create an array of bits.

    What you have is an array of chars, where each element contains an ASCII-encoded byte value:

    Array                _____ASCII Code______
    Element   Character  Hex  Decimal Binary
    data[0]  = '1'       0x31 49      00110001
    data[1]  = '0'       0x30 48      00110000
    data[2]  = '1'       0x31 49      00110001
    data[3]  = '1'       0x31 49      00110001
    data[4]  = '0'       0x30 48      00110000
    data[5]  = '1'       0x31 49      00110001
    data[6]  = '0'       0x30 48      00110000
    data[7]  = '1'       0x31 49      00110001
    data[8]  = '0'       0x30 48      00110000
    data[9]  = '0'       0x30 48      00110000
    data[10] = '1'       0x31 49      00110001
    data[11] = '1'       0x31 49      00110001
    data[12] = '1'       0x31 49      00110001
    data[13] = '1'       0x31 49      00110001
    data[14] = '1'       0x31 49      00110001
    data[15] = '@'       0x40 64      01000000
    data[16] = NUL       0x00  0      00000000
    If you want to interpret this string of characters as the bits of an integer, you need to:

    1. Decide what your bit-order is; ie, is data[0] the most- or least-significant bit?

    2. examine each character in turn and add the appropriate value to your total according its place-value in the binary number.

  • >> What are we doing wrong, or what requirements are you not stating properly, that has caused you to reject the feedback given thusfar? <<

    first time, excuse me for the multiposting!

    I started the first two threads from the office. But the Firm-Firewall noticed me, that my request cant be sent (this url is blocked from the Firewall). I was convinced, that my Postings are not arrived the Forum.
    Next day i started, this new thread from at home.

    >> Your use of "cast" here in a C forum is quite confusing, since "cast"....<<

    Sorry, I'v just been looking for the easiest way to convert a sequence of character, like example "11101" in a integer-Value. I solved this by dint of pointer before, but was looking for a better way.

    It was stupid from me, to confuse <cast> with <convert>, sorry!

    Thx for your help!

  • "Sorry, I'v just been looking for the easiest way to convert a sequence of character, like example "11101" in a integer-Value. I solved this by dint of pointer before, but was looking for a better way."

    There is no better way (to my knowledge) to convert a sequence of binary characters to an integer value than the ways that you've seen in your threads. That is, either explicitly convert while sequencing through your array (using a pointer or array index, and shift/add) or using strtol().

    Knowing that there is no better way, now you are well equipped to choose the method that suits you.

  • >> Knowing that there is no better way, now you are well equipped to choose the method that suits you. <<

    its certainly usefull!
    many thanks Dan!

  • 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'...

  • nice suggestion Andy! Thanks for your other postings too! They are very usefull.

  • Is this library function of any use to you?

    http://www.keil.com/support/man/docs/c51/c51_strtoul.htm

    In your case the base will be 2, of course.