Re: problem to convert integer to character

From: Fred Clare (fred AT ucar.edu)
Date: Fri Jan 21 2005 - 19:05:38 MST


Dear Li Tao,

You do not want "integertocharacter":

   The function integertocharacter converts integer values to
   character values.

   For any integer value in the range 0 to 127 (the ASCII decimal
   equivalent range for ASCII characters) the return value is the
   ASCII character having that decimal equivalent. Any value out
   of the range 0 to 127 is returned as a non-printing character.

NCL strings are sequences of individual characters terminated by
a NULL byte. For example in:

ncl 0> a = stringtocharacter("String")
ncl 1> print(a)

Variable: a
Type: character
Total Size: 7 bytes
             7 values
Number of Dimensions: 1
Dimensions and sizes: [7]
Coordinates:
(0) S
(1) t
(2) r
(3) i
(4) n
(5) g
(6)

you see that the string "String" has been converted to an array of
characters of size 7 (the extra character being the NULL character
that terminates the string).

Integers are automatically coerced to strings, see:

   http://ngwww.ucar.edu/ngdoc/ng/ref/ncl/NclDataTypes.html#Coercion

so I think what you want it something like:

ncl 0> a = (/20020201, 20020602, 20020203/)
ncl 1> b = new(dimsizes(a),string)
ncl 2> b = a
ncl 3> print(b)

Variable: b
Type: string
Total Size: 12 bytes
             3 values
Number of Dimensions: 1
Dimensions and sizes: [3]
Coordinates:
Number Of Attributes: 1
   _FillValue : missing
(0) 20020201
(1) 20020602
(2) 20020203

The variable "b" is now an array of strings. If you want to get
at the individual characters in the string, then you can use
the stringtocharacter function.

Fred Clare

> I have some problems to convert integer to character:
> I have a time array which value are 20020201, 20020602.....
> I use ncl function integertochar to try to convert them into character,
> but it doesn't work, could anyone help me about it? By the way, what is
> difference between character and string?
>
>
>
> do nf=0,kk
> time1=integertochar(time(nf))
> print(time(nf))
> print(time1)
> time_file_exists = systemfunc ("ls
> /disk/ide41/taoli/AMSR_nc/amsre_"+time1(0:7)+"v4.dat")
>
>
>
>
> Thanks a lot!
>
>
> Li Tao
>
> International Pacific Research Center
> School of Ocean and Earth Science and Technology
> University of Hawaii
> 1680 East West Road, POST Bldg., Room:412,
> Honolulu, HI, 96822
>
> Phone: 808-956-2453(O)
_______________________________________________
ncl-talk mailing list
ncl-talk@ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk



This archive was generated by hypermail 2b29 : Sat Jan 22 2005 - 21:53:04 MST