Convert from an array to a value?????

This is a discussion on Convert from an array to a value????? within the C Programming forums, part of the General Programming Boards category; I have a code that is this, it do work but i working on a IAR Embedded workbench IDE so ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    27

    Unhappy Convert from an array to a value?????

    I have a code that is this, it do work but i working on a IAR Embedded workbench IDE so now the problem is that the TXBUF0 can only read value. my output is a array so i need to know how to convert a array to a value?

    Code:
            num = 92160/pulseperiod;
            div1=16;
            while ((num>0)&&(div1>0))
    
            { ctr--;
            output[ctr]=dataset[(num%div1)];
            num=num/div1;
            }
            for (int i=1; i>=0; i--)
            output[i]=output[i+ctr];
            TXBUF0=output;
    thank if u can help me with this.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't "convert" an array. That would be like "digging" with a horse.

    Arrays hold values in contiguous memory locations - that's all they do.

    If you want a variable to be the same value as a certain location in the array then:

    my_variable = my_array[location];

    Location is usually called an "index".

    So something like:

    TXBUF0 = output[i];

    I'm not sure what you mean by "can only read" for TXBUF0.
    Last edited by Adak; 01-13-2010 at 01:32 AM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    27

    Unhappy

    now my output is a unsigned char output[2]; so how do i put it into the TXBUF0?

    is it something like TXBUF0=(output[1],output[0]); ???

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    for my TXBUF0, it only can accept value.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you want to add up the values in the output array, and put the total value into TXBUF0?

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    ya this is what i wanna do.i nid to put the two value together.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    TXBUF0 = output[0] + output[1];
    
    or
    TXBUF0 = 0;
    for(i = 0; i >= 0; i--) {
      TXBUF0 += output[i];
    }
    The loop works best if you have a lot of values.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    thank you i will try it.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    Code:
            num = 92160/pulseperiod;
            div1=16;
            while ((num>0)&&(div1>0))
    
            { ctr--;
            output[ctr]=dataset[(num%div1)];
            num=num/div1;
            }
            for (int i=1; i>=0; i--)
            output[i]=output[i+ctr];
             TXBUF0 = output[0] + output[1];
    i have tried this and i compile it show no error and warning. I just wanna clarify is the output the value of 92160/pulseperiod? Becos my main motive is to allow TXBUF to get the data of 92160/pulseperiod.

    thank

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have not showed the data type for num or pulseperperiod, so I don't know.

    If they're integers, then the remainder (if any), will be removed. If they're floats or doubles, then the remainder should be kept.

    Best thing to do is to work out some correct answers by hand, and then test those values in your program, and see if the answers it gives, match up with your correct ones.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    my num is declare as integer int num=0.
    Code:
    num = 92160/pulseperiod;
    92160/pulseperiod, the pulseperiod is a measurement taken through a heart rate board that measure our heart rate. so is a value. the program totally work. but their are still problem becos it can only sent out 2 digit number.whenever the heart rate go above 100. it will just show the behind 2 value which is 00. so i decide to change the decimal into hexadecimal.

    thank adak for taking up your time.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Edit: Never mind
    Last edited by Adak; 01-14-2010 at 11:16 PM.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    opps sorry i didnt clarify my question clearly sorry.cos now this code that im writing now is to put inside a heart rate board. so i need to sent out a hexadecimal value to my PC and i already written a vb code to convert hex back to decimal to my pc.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your code looks to me like it's meant to send out digits that have been stripped from the number, one at a time, using output [0] and output[1]. Basically, appending the two digits, onto each other, in the one TXBUF (transmit buffer I presume).

    so the number 123, becomes 7B, which is stripped apart to
    output[0] = 7, and output[1] = B

    then TXBUFF = "7B", as a string.

    I don't know if it works or not, or even makes any sense, because there's no way to test it, from the info you've given.

    You should have some data that you can test, no? You can't code a program in the dark, you have to have the range of data points so some plausible testing can at least, be done.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    27
    ya adak, i get what u mean. thank anyway for the great help. okie now i show u the previous work i done.it only can read 2 digit.

    Code:
       if(char_gen[(heartrate & 0xf0) >> 4]>4 || char_gen[(heartrate & 0xf00) >> 8]<2)
     
        TXBUF0 = 92160/pulseperiod; // Transmit buffer
    actually, it worked but the problem is that the board got a problem that TXBUF0 is a 8 bit so i can only read 2 digit. so anything mnore than 100 ,it will only take the last 2 digit. so i change the value to a hexadecimal so when the digit is more than 100 it will also be 2 digit. ya so it can read it than after that from the hexa than i will convert back to decimal again.

    Code:
        num = 92160/pulseperiod;
        div1=16;
        while ((num>0)&&(div1>0))
    
            { ctr--;
            output[ctr]=dataset[(num%div1)];
            num=num/div1;
        }
        for (int i=1; i>=0; i--)
        output[i]=output[i+ctr];
        TXBUF0 = output[1] + output[0];
    so this is the latest 1 i put inside tghe heart rate board. but it seem like it only transmit out the 1st data only. after that it hang.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert integer number to array elements
    By droseman in forum C Programming
    Replies: 11
    Last Post: 10-28-2009, 11:09 AM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. convert char array to int array
    By redruby147 in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 11:09 AM
  4. Replies: 2
    Last Post: 03-05-2005, 03:00 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21