Announcement

Collapse
No announcement yet.

Convert ascii of keyboard to unsigned long

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert ascii of keyboard to unsigned long

    How to convert ascii of keyboard to unsigned long value and save the results to the two variables (msb and lsb)?
    Code:
    ascii of keyboard := 37 35 30 30 30 = 75000;
    output expected as follows;
    Code:
    result_msb := 0x24F9;
    result_lsb := 0xFFFF;

    Thank You.

  • #2
    Have a look at str_GetD()
    Mark

    Comment


    • #3
      Hello mark,

      I tried with follows code, but I don't get the output I expected. I assign value of 999999 from keyboard(ascii value) to ascIn, but I don't get the correct output. Output expected is lsbOut =0x423F and msbOut=0x000F.

      Code:
      func caller()
         var buffer[4];
      
         ascToUlong(&kb[KB_MSG], &product0Window[PR2_PURCHASE_PRICE_LSB_T], &product0Window[PR2_PURCHASE_PRICE_MSB_T]);  
         to(buffer); print(&product0Window[PR2_PURCHASE_PRICE_LSB_T], &product0Window[PR2_PURCHASE_PRICE_MSB_T]);    // output error in here
         print (buffer);
      endfunc
      Code:
      func ascToUlong(var *ascIn, var *lsbOut, var *msbOut)
          var *p;
          var i;
          var dec[2];
          
          p := str_Ptr(ascIn);
          while(str_GetD(&p, &dec[i]) != 0) i += 2;
          lsbOut := dec[0];
          msbOut := dec[1];
      endfunc
      Last edited by rwin; 30th July 2015, 11:52 AM.

      Comment


      • #4

        while(str_GetD(&p, &dec[i]) != 0) i += 2; // What are you trying to do here? Surely you just want to retrieve 1 dword from the string

        lsbOut := dec[0]; // What are you trying to do here? You passed an address, so surely you want to write something
        msbOut := dec[1]; // into that address, not just change the address?

        to(buffer); print(&product0Window[PR2_PURCHASE_PRICE_LSB_T], &product0Window[PR2_PURCHASE_PRICE_MSB_T]); // What are you trying to do here? All you are doing is printing the address of two array elements
        Mark

        Comment


        • #5
          Hello mark,

          Yes, you are right, my code is mess, sorry for that. I modified the code as follows.

          My question, how to send out the dword value(32 bit) from ascToUlong function? Thank you.

          Code:
          func caller()
              // I GET WRONG RESULT IN HERE
              product0Window[PR2_PURCHASE_PRICE_LSB_T] := ascToUlong(&kb[KB_MSG]) & 0xFFFF;// get lsb
              product0Window[PR2_PURCHASE_PRICE_MSB_T] := ascToUlong(&kb[KB_MSG]) / 0xFFFF;// get msb    
          endfunc
          
          func ascToUlong(var *msg)
              var *p;
              var ulong[2];
              var buf[10];
          
              p := str_Ptr(msg);
              str_GetD(&p, ulong);   // 0-0xFFFFFFFF = 1 dword
          
              // I GET CORRECT RESULT IN HERE
              to(buf); print([HEX4]ulong[1], [HEX4]ulong[0]);
              print([STR]buf);
              
              return ulong;
          endfunc
          Last edited by rwin; 31st July 2015, 06:28 AM.

          Comment


          • #6
            You can only return a 16bit value, so you need something closer to your original code in that area.

            The comments should contain enough information for you to get it to work.

            You could make it easier by passing the address of the 'long' array and just updating the 2 integers in it.
            Mark

            Comment


            • #7
              Thank you mark for that clue, my problem is resolved.

              Code:
              func caller()
                  srcAddr := str_Ptr(ascToUlong(&kb[KB_MSG]));
                  str_PutWord(str_Ptr(&product0Window[PR2_PURCHASE_PRICE_LSB_T]), str_GetWord(srcAddr));
                  str_PutWord(str_Ptr(&product0Window[PR2_PURCHASE_PRICE_MSB_T]), str_GetWord(srcAddr + 2)); 
              end func
              Best Regards

              Edha

              Comment

              Working...
              X