Home AMX User Forum AMX General Discussion
Options

little endian question

hi together

i dond't understanding some hex math... :-)
i need "adding" 2 bytes like $FC $01 . i receive this by my HVAC system for temp value.
no i must this hex codes changes in $1FC for the right value.

how can i do that in NetLinx?

thanks for some help.

john

Comments

  • Options
    Jorde_VJorde_V Posts: 393
    Ok first off,

    0xFC + 0x01 = 0xFD

    Edit: (Clarifying HEX adding)
    HEX (base 16) uses the numbers 0-9 (10 numbers) and A-F (6 letters).

    0x00 = 0
    0x01 = 1
    0x02 = 2
    0x03 = 3
    0x04 = 4
    0x05 = 5
    0x06 = 6
    0x07 = 7
    0x08 = 8
    0x09 = 9
    0x0A = 10
    0x0B = 11
    0x0C = 12
    0x0D = 13
    0x0E = 14
    0x0F = 15
    0x10 = 16
    0x11 = 17
    ...

    The above is the Hex value (base 16) in Decimal value (base 10)
    In any case.

    0x14 + 0x01 = 0x15 (21 in decimal)
    0x14 + 0x0A = 0x1E (30 in decimal)

    0x is generally used to specify a HEX Value. However in netlinx this is $.

    Now for your HVAC system you will need to do the following:
    define_function hvac()
    {
        integer result;
        integer one; // First Byte
        integer fc;    // Second Byte
        integer decresult; // decimal result
    
        one = $01;    // Use your feedback for the 1st byte
        fc = $FC;      // Feedback from 2nd Byte
    
        result = fc + one;
    
        send_string 0,"'Result: ',result,' in HEX'";
    
        decresult = hextoi("result");
        send_string 0,"'Result: ',decresult,' in DEC'";
    }
    

    This sends the following to your master (use diagnostics to see):

    Result: $FD in HEX

    That's the HEX part of it.

    You named to topic Little Endian Question, so I'll try explaining that as well.

    Little Endian means Least Significant Byte (or LSB). So the Byte starts at the LSB. The byte should be read right-to-left.

    Going by that order a byte would look like this (read RTL):

    $07, $06, $05, $04, $03, $02, $01, $00

    Each HEX Value representing a bit, a byte consists of 8 bits. So it starts with $00 and ends with $07

    I hope this helps you out.
  • Options
    i need "adding"

    Since "Adding" is in quotes, I assume you just need to combine the two numbers. If that's what you need it's pretty simple. You would just need to take the leading digit and multiply by $100, then add the second digit. In your example it would be
    nCombinedNumber = ($01 * $100) + ($FC)

    Think of it in terms of decimal numbers...

    In base-10 if you received a "2", then a "3" and you wanted to combine them to create the number "23" you would just put them in their proper "place value". First column would be 10^0, second column would be 10^1, etc.
    You would take the 2, multiply by 10^1 (second column) then take the 3 and multiply by 10^0 (first column), then add them together.
    In your hex case, it's $01 x ($10^1) + $FC x ($10^0).

    --John
  • Options
    truetrue Posts: 307
    Language barrier. Your replies are very likely not what he means.

    integer newtemp = temp[1] & (temp[2] << 8)
  • Options
    hi all together

    thanks for your answers and codes!

    it's correct, i need to combine these two hex's ;-)

    with ($01 * $100) + ($FC) it works perfect.

    thanks

    john
  • Options
    Glad it worked out.

    True's LSHIFT is a more elegant way of doing it, it shifts the first number 8-bits over to the correct place (same thing as what the math accomplishes) thus making a 2-byte number also.

    -John
  • Options
    Glad it worked out.

    True's LSHIFT is a more elegant way of doing it, it shifts the first number 8-bits over to the correct place (same thing as what the math accomplishes) thus making a 2-byte number also.

    -John

    thanks for the notice!

    john
Sign In or Register to comment.