Mobiiliohjelmointi/Mobile programming

 

Exercise 3 – Descriptors

 

This exercise focuses on how to use descriptors. Descriptors are classes for handling text and binary data.

Download this application (zip file) that is used as a basis for the exercise.

Following the instructions of exercise 2, import the project DescConcole (DescriptorsConsole).

 

/**********************************************************************************

*                                                                                                                                                                                   *
*              For further information on descriptors, please see www.newlc.com  -  String and Descriptors*

*                                                                                                                                                                                   *

**********************************************************************************/

TBuf, TBufC descriptors, and the _LIT macro.

  1. Declare three constant literals using the _LIT macro. First literal contains the text "This ", the second "is " and the third "a descriptor".
  2. Declare a TBuf descriptor with maximum length of 30 characters
  3. Assign the first literal text to the TBuf.
  4. Append the second and third literal text to the TBuf
  5. Print the new text to the console using console->Printf function.
  6. Change the TBuf to TBufC. Try compiling -- you will notice that you cannot Append to a TBufC, since it is now a constant descriptor. You have to use TPtr to modify the contents of a constant descriptor.

TBufC and TPtr

  1. Change the TBuf to TBufC (if not already).
  2. Obtain a pointer descriptor (TPtr) to the data inside the TBufC, by calling TDesC's Des().
  3. Use that pointer descriptor to modify the data, as in the previous task.
  4. Print the new text to the console using console->Printf function.

TDes::Format

  1. Declare a literal text containing the text "My age is %d and my name is %S".
  2. Declare a literal text containing your name.
  3. Declare a TBuf with a suitable length.
  4. Use TDes::Format so that the first parameter is the first literal text, second parameter is your age (an integer) and the third parameter is the literal text with your name.
  5. Print the new text to the console using console->Printf function.

HBufC descriptor

  1. Declare a TBuf descriptor formatStr containing "My age is %d".
  2. Allocate HBufC with NewLC, with size of formatStr.Length() + 3.
  3. Get a TPtr to this HBufC, using TDesC::Des().
  4. Use the TDes::Format with the TPtr, to format the text into the TPtr, using the formatStr as the first parameter to the Format, and a number as the second parameter.
  5. Print text to the console.
  6. Remember to remove from cleanup stack and deallocate everything that was allocated and put into the cleanup stack!!