Sun ""JavaDoc" http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html 1.15 has expanded support for the java.lang.String package
String Storage FormatsThere are two forms of String storage. FLASH Storage, HEAP Storage. FLASH Storage - FlashStringsWhen a String is created with a String literal then a FLASH storage format string is created. In this format, the String object contains a FLASH address location of the String. The FLASH Stored string is stored in 16Bit word format, in 18F devices and 14Bit Word format in 16F device. When stored in 16Bit word format this is compatible with unicode 16Bit Strings. A literal String can be up to 65536 words long which allows literal Strings to be used very efficiently as binary resources. The charAt() method will return a 16bit char which should be cast to a int to There are several techniques you can use to use Strings effectively
String Access
Examples The String "HI" = { 0x48, 0x 0x49 }
String hi = "HI";
//This stores 0x0048, 0x0049
String hiUnicode = "\u0048\u0049";
//This stores 0x0048, 0x0049
String hiCompressed = "\u4948";
//Stores "HI" in a single word
System.out.print( hi ) ; //Prints Hi
System.out.print( hiUnicode ) ; //Prints Hi
int compressed = hiCompressed.charAt(0);
System.out.print( (char) (compressed & 0xFF) ); //Prints H
System.out.print( (char) ((compressed >> 8) & 0xFF) ); //Prints i
FlashRAW16InputStream in = new FlashRAW16InputStream(hiCompressed) ;
while( true ){
int c = in.read();
if( c == -1){
break;
}
System.out.print( (char) c ); //Prints H,I
}
HEAP Storage - HeapStringsStrings can also exist in HEAP memory. Since muvium devices typically have very limited heap it is prefered to only use the Heap for temporary storage of the String, eg computed Strings and then allow the garbage collector to free the string. So it is not a good idea to cache HEAP strings for later use, it is better to dynamically create them, use them, then loose them. This is a general philosophy when using muvium. A Heap string is a more standard string in that it contains a reference to an array of bytes. Heap strings are created when you use a constructor passing an array or when a String is computed using the String.valueOf() or Object.toString(). Heap strings use a char[] which is in fact an 8 bit array. This is different to the java standard of 16 bit char[] but due to the extreme resource limits of the Heap the 8 bit char was chosen. This covers the regular ASCII range and is rarely an issue. String requiring 16Bit unicode format should use the Literal Flash format.
Examples Creating HeapStrings
//Creates a new HeapString
String hi = new String( new byte[]{'H','I'} );
//Creates a StringBuffer with FlashStrings
StringBuffer buf = new StringBuffer();
buf.append( "HI" );
buf.append( "HI" );
//Do this to keep FlashStrings in Flash while printint out ( efficient )
for(int i = 0; i < buf.length() ; i++){
System.out.print( (char) buf.charAt(i) );
}
//This creates HeapString 'HIHI'( CAREFUL!! easy to create large HeapStrings )
String hi2 = buf.toString();
String hi3 = String.valueOf( 100 );
System.out.println( hi );
System.out.println( hi2 );
System.out.println( hi3 );
HintsUsing Strings with printlnThe println, from PrintStream and PrintWriter appends a LF ( chr(10) ) to the end of a String when it prints. This may not be compatible with the way you view the Strings, for example Hyperterminal by default uses CRLF chr(10) + chr( 13 ). Here are some tips for getting the correct formatting
//Add the correct formating directly to the String
System.out.print("PrintMe\r\n"); //Add the crlf directly
System.out.println("PrintMe\r"); //Add the cr and allow the LF to be appended by println
System.out.print("\r\n\prepend the clrf");
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||