Home AMX User Forum AMX Technical Discussion

DEFINE_CONSTANT CHAR[] in HEX

Dear Godfathers of AMX!

How can i define a CHAR[] constant with hex characters?

What i want is:

DEFINE_CONSTANT
CHAR MY_CONSTANT[3] = " $02 , $03 , $0D "


I do it like:

DEFINE_VARIABLE
CHAR MY_CONSTANT[3]

DEFINE_START
MY_CONSTANT = "$02,$03,$0D"


But there MUST be a more elegant way!?
So how can i define a CHAR[] constant with HEX characters directly in the DEFINE_CONSTANT section?

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    You can do it this way:

    DEFINE_CONSTANT
    CHAR MY_CONSTANT[3] = {$02,$03,$0D}

    or skip the bounds and do it like this:

    DEFINE_CONSTANT
    CHAR MY_CONSTANT[] = {$02,$03,$0D}
  • mhermannsmhermanns Posts: 32
    light bulb moment...

    Hi Joe!

    And ones again: thank you!
  • Joe HebertJoe Hebert Posts: 2,159
    Sure thing. Micha. Have fun. :)
  • mhermannsmhermanns Posts: 32
    ...and when the constant is a combination of ascii and hex characters???

    e.g. 'PON',$0D,$0A
  • jjamesjjames Posts: 2,908
    Your example:
    CHAR PLASMA_CMDS[][13]=
    {
    	 {"'PON',$0A,$0D"}
    }
    

    Another example (Integra CDC-3.4)
    CHAR CD_CMDS[][10]=
    {
    	 {"'!4PLY',CR"}
    	,{"'!4STP',CR"}
    }
    
    
  • mhermanns wrote:
    ...and when the constant is a combination of ascii and hex characters???

    e.g. 'PON',$0D,$0A

    In case of doubt:
    DEFINE_CONSTANT
    CHAR MyString[] = { 'P','O','N',$0D,$0A }
    
  • mhermannsmhermanns Posts: 32
    Thanks Marc,

    this is exactly what i was looking for!
    DEFINE_CONSTANT
    CHAR MY_CONSTANT[4] = {"'PON',$0D" } -> ERROR
    
    DEFINE_CONSTANT
    CHAR MY_CONSTANT[4] = {'P','O','N',$0D} -> OK
    
  • jjamesjjames Posts: 2,908
    Hmm . . . my example compiles fine . . . though I didn't test it actually leaving the master and going to the device. Skin your cat the way you please. :)

    BTW - What kind of error are you getting? The TOO MANY ELEMENTS IN INITIALIZER error or a different one. You would need to increase your size to 9 for your example.
    DEFINE_CONSTANT
    CHAR TEST[9] = {"'PON',$0D"}  // -> COMPILES FINE
    
  • mhermannsmhermanns Posts: 32
    Yes, it's the TOO MANY ELEMENTS IN INITIALIZER error.

    Why do i need an array of size 9? Actually i have just 4 characters??
  • jjamesjjames Posts: 2,908
    Good question . . . I think this one is for the Programming Gurus. I'm one of those that if it says it's not big enough, I increase the number until it is. Haha. . .
  • DHawthorneDHawthorne Posts: 4,584
    mhermanns wrote:
    Thanks Marc,

    this is exactly what i was looking for!
    DEFINE_CONSTANT
    CHAR MY_CONSTANT[4] = {"'PON',$0D" } -> ERROR
    
    DEFINE_CONSTANT
    CHAR MY_CONSTANT[4] = {'P','O','N',$0D} -> OK
    

    The error is because of the double quotes; not needed when initializing, only if assigning a value to a variable, which you can't do here anyway becvause it's a CONSTANT. CHAR { 'P', 'O', 'N', $0D } does it. Lumping the PON together in a single quote provokes a string-to-CHAR warning, but I think it is benign - I just hate to leave compiler warnings unheeded. There is also no need to put a value in the square brackets, it sizes according to the initializer, and since it's a CONSTANT, never changes.
  • Constant Hex Strings

    In the good 'ol Axcess days you could write:
    DEFINE_CONSTANT

    PROJ_ON = "'PON' $0D"

    very readable and consistent with the way you build strings elsewhere.

    but this feature wasn't carried through to Netlinx, which made upgrading Axcess code to netlinx real fun, not to mention writing device drivers that work on both platforms.

    AMX told me the original capability of having non-printable chars in a constant string was "a mistake", so it was removed in the upgrade. The options in Netlinx are the unreadable: string of chars inside curly braces { 'P', 'O', 'N', $0D } or the illogical: use constant variables (a contradiction in terms by any measure).

    I prefer to declare the strings as variables, using capitals, and initialize them in DEFINE_START. By convention in C, things in capitals are constants and never appear to the left of the assignment operator (=)


    IMHO the Axcess version was much better, and I wish they'd kept it.
Sign In or Register to comment.