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.
- Declare three constant literals using the _LIT
macro. First literal contains the text "This ", the second
"is " and the third "a descriptor".
- Declare a TBuf descriptor with maximum length of
30 characters
- Assign the first literal text to the TBuf.
- Append the second and third literal text to the
TBuf
- Print the new text to the console using
console->Printf function.
- 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
- Change the TBuf to TBufC (if not already).
- Obtain a pointer descriptor (TPtr) to the data
inside the TBufC, by calling TDesC's Des().
- Use that pointer descriptor to modify the data,
as in the previous task.
- Print the new text to the console using
console->Printf function.
TDes::Format
- Declare a literal text containing the text
"My age is %d and my name is %S".
- Declare a literal text containing your name.
- Declare a TBuf with a suitable length.
- 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.
- Print the new text to the console using
console->Printf function.
HBufC descriptor
- Declare a TBuf descriptor
formatStr containing "My age is %d".
- Allocate HBufC with NewLC, with size of
formatStr.Length() + 3.
- Get a TPtr to this HBufC, using TDesC::Des().
- 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.
- Print text to the console.
- Remember to remove from cleanup stack and
deallocate everything that was allocated and put into the cleanup stack!!