Home AMX User Forum AMX Technical Discussion

Integer to Char conversion?

I have another type conversion question:
DEFINE_VARIABLE
SINTEGER PercentValue
SLONG ValueToSend

DEFINE_START
PercentValue = -10
ValueToSend =  PercentValue * 65536 

I need ValueToSend to be a Char that looks like: ( 0xFF 0xF6 0x00 0x00 )
PercentValue ranges from -100 to +100

Basically I'm trying to go from 0xFFF600 to {0xFF, 0xF6, 0x00}

Sidenote: why does the above code get this warning?
WARNING: C10571: Converting type [SINTEGER] to [LONG]
I didn't define ValueToSend as SLONG for my health?

Comments

  • DHawthorneDHawthorne Posts: 4,584
    A signed integer only goes from -32768 to +32768. You are doing an arithmetic operation on an SINTEGER (PercentValue) with a value of 65536, which forces the compiler to attempt to type cast it into a LONG. Since there is a potential data loss here (the signed part), and the compiler isn't bright enough to typecast them to SLONG instead of LONG for the operation, it generates the error.

    Try first making PercentValue an SLONG; that may be enough to fix it. If you need that to be an SINTEGER for other parts of the code and the conversion will mess you up, assign it to a temporary value with the TYPE_CAST function and do your math on that.

    This is a case of automatic type conversions not doing what you expect. In all such cases, you have to force the issue so the compiler will do what you want. Some of the conversion rules are pretty arcane, and it often take a bit of fiddling to get what you want out of it. The rules as posted in the help file, are incomplete and somewhat vague in places.
  • travistravis Posts: 180
    DHawthorne wrote: »
    Some of the conversion rules are pretty arcane, and it often take a bit of fiddling to get what you want out of it. The rules as posted in the help file, are incomplete and somewhat vague in places.

    that's why I'm here. Thanks for your help.

    I tried to simplify even more to figure out what is going on. Now I'm dismayed:
    DEFINE_VARIABLE
    SLONG BigSignedNumber
    
    DEFINE_START
    BigSignedNumber = -65536
    
    WARNING: Main.axs(22): C10571: Converting type [LONG] to [SLONG]  
    
    

    wat?
  • DHawthorneDHawthorne Posts: 4,584
    Chalk one up for the automatic conversions just plain failing.

    BigSignedNumber = TYPE_CAST(-65536)

    works, though by all rights you shouldn't have to do it. Apparently, it's not seeing the - sign when evaluating the expression for conversion.
  • travistravis Posts: 180
    DHawthorne wrote: »
    Chalk one up for the automatic conversions just plain failing.

    BigSignedNumber = TYPE_CAST(-65536)

    works, though by all rights you shouldn't have to do it. Apparently, it's not seeing the - sign when evaluating the expression for conversion.

    I just noticed
    BigSignedNumber = -65535
    doesn't cause the warning


    anyways, here's how i ended up converting from integer to character:
    integer num
    char ab, cd
    char result[2]
    
    num = 14702
    ab = type_cast( num  / 256 )
    cd = type_cast( (num  << 8) / 256 )
    
    result = "ab, cd"
    
  • travistravis Posts: 180
    ungh

    I'm really stuck. I can't get from a Signed Long to a string .

    I need be able to send the number 0xFFF60000 or 0x00A00000 out to a device including all the leading and trailing zeros.

    For example would look like "FF, F6, 00, 00" going out through send_string
  • jweatherjweather Posts: 320
    Please post the relevant bit from the protocol manual so I can make sure this is really what you want to do.

    To send out an SLONG as four bytes (assuming big-endian byte order):
    SLONG value
    LONG temp
    
    temp = type_cast(value)
    SEND_STRING dv, "temp>>24, (temp>>16)&$FF, (temp>>8)&$FF, temp&$FF"
    
  • travistravis Posts: 180
    ahh, thanks. I tried the exact same thing except with the temp variable and type conversion. It works. The device is responding.


    anyways, here's the relevant part of the protocol
    To convert to this data type, perform the following conversion:
    ValueToSend = PercentageValue * 65536
    e.g.
    10% = 655360 ( 0x00 0x0A 0x00 0x00 )
    12.5% = 819200 ( 0x00 0x0C 0x80 0x00 )
    50% = 3276800 ( 0x00 0x32 0x00 0x00 )
    100% = 6553600 ( 0x00 0x64 0x00 0x00 )
    -10% = -655360 ( 0xFF 0xF6 0x00 0x00 )
    -12.5% = -819200 ( 0xFF 0xF3 0x80 0x00 )
Sign In or Register to comment.