Home AMX User Forum NetLinx Studio

ASCII value of a CHAR

Is there a function in Netlinx world to return the ASCII value of a CHAR? Most languages I've used have a built in function for this but I don't find an equivalent in the help file.

I am trying to parse some RDS Radio text and the manufacturer has a ridiculous command format where several "control characters" are reserved and so if you want to use one you have to bitwise OR the value with 64 and use the result instead of the actual value.

Needless to say, when feedback comes from the tuner I have to XOR certain characters to get the intended value.

So I can get the character just fine but now I need to get it's ASCII value (from within my code) to do the manipulation.



PS: I've tried type casting the char to an integer value but oddly enough it doesn't return the ASCII code.

Comments

  • Spire_JeffSpire_Jeff Posts: 1,917
    Not sure if this helps, but you could do this type of comparison:

    Select{
    Active(nChar[x] == "13"):
    Active(nChar[x] == "14"):
    Active(nChar[x] == "21"):
    Active(nChar[x] == "25"):
    Active(nChar[x] == "29"):
    {
    nChar[x] = nChar[x] BXOR $FF //Or whatever you need to XOR it with
    }
    }

    Obviously, you would need a loop to traverse the data, and I just pulled the ASCII values out of my butt, but I think this will work for what you need.

    Jeff
  • BigsquatchBigsquatch Posts: 216
    Spire_Jeff wrote: »
    Not sure if this helps, but you could do this type of comparison:

    Select{
    Active(nChar[x] == "13"):
    Active(nChar[x] == "14"):
    Active(nChar[x] == "21"):
    Active(nChar[x] == "25"):
    Active(nChar[x] == "29"):
    {
    nChar[x] = nChar[x] BXOR $FF //Or whatever you need to XOR it with
    }
    }

    Obviously, you would need a loop to traverse the data, and I just pulled the ASCII values out of my butt, but I think this will work for what you need.

    Jeff


    What I need is a function that returns the ASCII value of any specific character.

    Hypothetical example:
    INTEGER theASCIIvalue
    char aCharacter 
    aCharacter = LEFT_STRING(DATA.TEXT, 1)
    
    theASCIIvalue = someUnknownFunction(aCharacter)
    
    
    

    In the example let's say aCharacter is 'A' , I need a function that returns 65, the ASCII value of 'A'
  • Spire_JeffSpire_Jeff Posts: 1,917
    This code works:
    DEFINE_VARIABLE
    INTEGER nASCII 
    INTEGER nPOINTER = 0
    CHAR TEXT[20]
    
    
    DEFINE_START
    TEXT = "13,'A Test String',10,13"
    
    DEFINE_EVENT
    CHANNEL_EVENT[dvTEST,1]{
      ON:{
    	IF(nPOINTER < LENGTH_ARRAY(TEXT))
    		nPOINTER++;
    	ELSE 
    		nPOINTER = 1;
    					
    	nASCII = TYPE_CAST(TEXT[nPOINTER]);
      }
    }
    

    If you watch the variables in debug and control the channel ON/OFF, you will see that the nASCII value is updated correctly. 65 for A and so forth.

    Jeff
  • Joe HebertJoe Hebert Posts: 2,159
    Bigsquatch wrote: »
    In the example let's say aCharacter is 'A' , I need a function that returns 65, the ASCII value of 'A'
    One way is to TYPE_CAST what you need For example:
    DEFINE_DEVICE
    
    dvTP = 10001:1:0
    
    DEFINE_TYPE
       
    DEFINE_EVENT
    
    BUTTON_EVENT[dvTP,1] {
    
       PUSH: {
       
          CHAR cTest[3]
          INTEGER num
          
          cTest = 'ABC'
          
          num = TYPE_CAST(cTest[1])
          
          SEND_STRING 0, "'cTest[1] = ',cTest[1],' ,numerical value = ',ITOA(num)"
       }
    
    }
    

    The output when button 1 is pushed:
    Line     62 :: cTest[1] = A ,numerical value = 65 - 16:21:38
    
  • mpullinmpullin Posts: 949
    Bigsquatch wrote: »
    What I need is a function that returns the ASCII value of any specific character.
    I wrote this little guy real quick: might be a good start:
    DEFINE_FUNCTION INTEGER atov(CHAR c){
        RETURN TYPE_CAST("c")
    }
    
    DEFINE_PROGRAM
    wait 20 SEND_STRING 0, "'atov test:',itoa(atov('a'))";
    // produces:
    // Line     33 (16:23:55):: atov test:97
    
  • BigsquatchBigsquatch Posts: 216
    Joe Hebert wrote: »
    One way is to TYPE_CAST what you need For example:

    You are right, it does just what i needed.

    I had tried TYPE_CAST earlier but I must have had some other issue in my code because it didn't look like it was working.


    Unfortunately my approach to parsing the feedback was overly optimistic and I'm going to have to delve into checksums and all sorts of bitwise manipulation if I'm going to get it to work.

    Here's a quote from the protocol description:
    Checksum Calculations

    Control characters 0 and 4 through 19 have been reserved for future expansion. Any data values that are to be sent that correspond to a control character must be encoded with the following method:

    1. Send the flag control character.
    2. Send the Data value bitwise ORed with the value 64.

    The checksum is simply the inverse (ones compliment) of the sum of the Command and Data bytes of the packet.

    For example, if the Command byte was 20 and the Data byte was 20 then the checksum would be 215. (20 + 20 = 40, compliment of 40 = 215)

    When the data values are encoded because they overlap with a control character, the actual value should be used in the sum and not the encoded value.

    Unbelievable that someone had the nerve to put the word 'simply' in that paragraph. :o


    Thanks for the help folks.
  • jweatherjweather Posts: 320
    One's complement of the sum? That's about as "simple" of a checksum as it gets. I guess it would be simpler if you didn't do the one's complement... They get plenty more complex, believe me. Encoding "special" data values is annoying, but using flag characters is a common solution -- see Russound, among others.
  • Was this an NAD piece?
  • ericmedleyericmedley Posts: 4,177
    You might try this.

    create another variable that is an integer.

    Then do this statement


    nInteger=cCharactor[1]

    You might get a warning on compile. but it might work.
  • Thanks for the suggestion.

    I gave up on this problem however after I was getting usable text from several stations and then noticed that another station's data was different and contained incorrect codes according to NAD's formatting info.
  • GSLogicGSLogic Posts: 562
    Was this an NAD piece?

    I hate the old NAD junk, the new models have a very simple ascii protocol that works well.
  • GSLogic wrote: »
    I hate the old NAD junk, the new models have a very simple ascii protocol that works well.

    Well has not been simple for my I am having issues with the data sent back for volume, bass, and trebel levels. I know that when I get 241 for the volume it means it is at -15 but I can not for the life of me get that to happen in the code. Even using tech note 457 I can not get the checksums to pan out.
    Take the examples in the protocol to query the device 1,20,20,2,215. I try to get the compliment for 40 and get six thousands something using this instead of the 215.
    x = 40
    x = (BNOT x) + 1
    
Sign In or Register to comment.