Difference between revisions of "Text manipulation with GAMAP"

From Geos-chem
Jump to: navigation, search
(New page: == Strings and bytes == In IDL, a string is equivalent to an array of byte values. A byte is a collection of 8 bits and may express values from 0-255. The [http://www.asciitable.com/ AS...)
 
Line 1: Line 1:
 
== Strings and bytes ==
 
== Strings and bytes ==
  
In IDL, a string is equivalent to an array of byte values.  A byte is a collection of 8 bits and may express values from 0-255.  The [http://www.asciitable.com/ ASCII collating sequence] has 255 values.   Each value of the byte represents a single text character.
+
In IDL, a string is equivalent to an array of byte values.  A byte is a collection of 8 bits and may express values from 0-255.  The [http://www.asciitable.com/ ASCII collating sequence] has 255 values. (Actually, the original ASCII table had 128 values, but this was later extended to 255 values to include special characters.)  Each value of the byte represents a single text character.
  
 
This means that it is easy to convert between strings and bytes.  If you have an array of bytes, you can use any of the IDL string routines on them, for example:
 
This means that it is easy to convert between strings and bytes.  If you have an array of bytes, you can use any of the IDL string routines on them, for example:
Line 20: Line 20:
 
   73  68  76  32 105 115  32 110 101  97 116  33
 
   73  68  76  32 105 115  32 110 101  97 116  33
  
 
+
== Replacing characters in a string ==
== Replacing characters in a string==
+
  
  
  
 
== Testing ==
 
== Testing ==

Revision as of 19:32, 15 April 2008

Strings and bytes

In IDL, a string is equivalent to an array of byte values. A byte is a collection of 8 bits and may express values from 0-255. The ASCII collating sequence has 255 values. (Actually, the original ASCII table had 128 values, but this was later extended to 255 values to include special characters.) Each value of the byte represents a single text character.

This means that it is easy to convert between strings and bytes. If you have an array of bytes, you can use any of the IDL string routines on them, for example:

IDL> byte_array = [ 72B, 69B, 76B, 76B, 79B ]
IDL> help, byte_array    
BYTE_ARRAY      BYTE      = Array[5]
IDL> print, strtrim( byte_array, 2 ) 
HELLO

GAMAP comes with a very useful routine called str2byte.pro. This allows you to take a text string and to convert it into the equivalent array of bytes.

IDL> str = 'IDL is neat!'
IDL> byte_array = str2byte( str, strlen( str ) )
IDL> help, byte_array
BYTE_ARRAY      BYTE      = Array[12]
IDL> print, byte_array   
  73  68  76  32 105 115  32 110 101  97 116  33

Replacing characters in a string

Testing