Home AMX User Forum NetLinx Studio

16 bit to 8 bit

I would like an example of converting from an 8 bit integer to a 16 bit integer and vice versa. I just have to convert a level back and forth.

Thanks,
Ryan

Comments

  • AMXJeffAMXJeff Posts: 450
    ryanww wrote:
    I would like an example of converting from an 8 bit integer to a 16 bit integer and vice versa. I just have to convert a level back and forth.

    Thanks,
    Ryan

    No conversion needed...
    DEFINE_VARIABLE
    
    // 8 BIT VARIABLE
    CHAR cTest
    
    // 16 BIT VARIABLE
    INTEGER nTest
    
    DEFINE_PROGRAM
    
    // 8 Bit variable to 16 bit variable - No Code warnings.
    nTest = cTest // (Binary) Coverts 1111 1111 to 0000 0000 1111 1111
    
    // 16 Bit variable to 8 bit variable - Code Warning, use Type_Cast
    cTest = TYPE_CAST(nTest) // (Binary) Coverts 1111 1111 1111 1111 to 1111 1111
    
    
    
  • ryanwwryanww Posts: 196
    I don't know this would work for what I need. I have a structure with 500 positions of integers. All of them are integers from 0-65535 which is a level value of which a device uses. I need to know how to convert a 0-255 level that is sent from a g3 touch panel and convert that value to what the equivalent of 1-65535. So if the touch panel sent a level of 127, it needs to store that as 32767 in the structure integer.

    Example:

    LEVEL_EVENT [dvBkStoreG3Web,1] //Inside Master Fader
    {
    SEND_STRING dvSymnetBS,"'CS 421 ',ITOA("(LEVEL.VALUE)16 bit"),$0D"
    SYM_CONTROL[421].POSITION = (LEVEL.VALUE)16bit
    }

    SEND_LEVEL dvBkStoreG3Web,1,(SYM_CONTROL[421].POSITION)8 bit //Inside Level
  • AMXJeffAMXJeff Posts: 450
    Oh, ok...........

    Simple formula that works for all ratio conversions. Does not matter if your going from a large range to a smaller range, or vice versus, this formula works... The only rub is that the ranges have to be zero to x. If they are not you need to subtract or add to make the ranges 0 to x.

    answer = ((max_converted_value * current_value) / max_current_value)

    // coverting to a 0 - 65535 range,starting with a 0 to 255 range;
    answer = ((65535 * 127) / 255);

    // coverting to a 0 - 255 range, starting with a 0 to 65535 range;
    answer = ((255 * 32767) / 65535);

    // coverting to a 0 -255 range, starting with a -100 to 20 range;
    answer = (255 * (current_value + 100)) / 120);

    // coverting to a 0 -255 range, starting with a 20 to 80 range;
    answer = (255 * (current_value - 20)) / 60);
    ryanww wrote:
    I don't know this would work for what I need. I have a structure with 500 positions of integers. All of them are integers from 0-65535 which is a level value of which a device uses. I need to know how to convert a 0-255 level that is sent from a g3 touch panel and convert that value to what the equivalent of 1-65535. So if the touch panel sent a level of 127, it needs to store that as 32767 in the structure integer.

    Example:

    LEVEL_EVENT [dvBkStoreG3Web,1] //Inside Master Fader
    {
    SEND_STRING dvSymnetBS,"'CS 421 ',ITOA("(LEVEL.VALUE)16 bit"),$0D"
    SYM_CONTROL[421].POSITION = (LEVEL.VALUE)16bit
    }

    SEND_LEVEL dvBkStoreG3Web,1,(SYM_CONTROL[421].POSITION)8 bit //Inside Level
  • Just curious, I am asking because I don't know the answer.

    Since the processor can handle a LONG - 32bit integer I would assume that the operation (65535 * 127) would not overflow the internal operation of the processor. But what if...

    I can not think of a real application, but what if you wanted to end up with a range from 0 - 4294967295 (FFFFFFFF) would the initial operation of (4294967295 * 127) overflow the internal operation of the processor? what is the limit of the processor's mathematical operations?
  • AMXJeffAMXJeff Posts: 450
    Very good question the answer is that the math will be forces to stay within the 32bits.

    // So this should be true, but netlinx will keep the answer under 32 bits

    $FFFFFFFF = (($FFFFFFFF * 2) / 2))

    // but netlinx does this
    FFFFFFFE = ($FFFFFFFF * 2);
    7FFFFFFF = ($FFFFFFE / 2);

    // so this is the answer, its wrong, but the answer none the less.
    $7FFFFFFF = (($FFFFFFFF * 2) / 2))
    Just curious, I am asking because I don't know the answer.

    Since the processor can handle a LONG - 32bit integer I would assume that the operation (65535 * 127) would not overflow the internal operation of the processor. But what if...

    I can not think of a real application, but what if you wanted to end up with a range from 0 - 4294967295 (FFFFFFFF) would the initial operation of (4294967295 * 127) overflow the internal operation of the processor? what is the limit of the processor's mathematical operations?
Sign In or Register to comment.