Re: stringtochar function

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Mon Jul 09 2012 - 10:19:55 MDT

I think Dave will have to explain why the NULL character is necessary.

If you just want the "1", "2", "3", then you will have to truncate the array yourself:

ncl 0> s = "123"
ncl 1> c = tochar(s)
ncl 2> print(c)

Variable: c
Type: character
Total Size: 4 bytes
            4 values
Number of Dimensions: 1
Dimensions and sizes: [4]
Coordinates:
(0) 1
(1) 2
(2) 3
(3) 0x00
ncl 3> cnew = c(:dimsizes(c)-2)
ncl 4> print(cnew)

Variable: cnew
Type: character
Total Size: 3 bytes
            3 values
Number of Dimensions: 1
Dimensions and sizes: [3]
Coordinates:
(0) 1
(1) 2
(2) 3
ncl 5>

On Jul 9, 2012, at 10:11 AM, Xin Xi wrote:

> Mary,
> I am not trying to get the length in this case. I am trying to get the "correct" char array converted from the given string, e.g., s="123", I want get c=(/"1", "2", "3"/) without the NULL char. And I still didnot get it why there should be a NULL in the end. Thanks.
>
> On Mon, Jul 9, 2012 at 11:45 AM, Mary Haley <haley@ucar.edu> wrote:
> The character you're seeing is a NULL character. This will always be appended when you call stringtochar.
>
> I believe the reason for this NULL character is for the case where you give it an array of strings. Since each string may be a different length, it becomes
> necessary to have something that denotes the actual end of each string
>
> To correctly get the string length, you need to subtract 1:
>
> s = "123456789"
> c = stringtochar(s)
> clen = dimsizes(c) -1
> print("length of c = " + clen)
>
>
> (0) length of c = 9
>
>
> --Mary
>
> On Jul 8, 2012, at 11:27 PM, Xin Xi wrote:
>
> > Hi there,
> > when I use the stringtochar function, the resulting char array has an ending element of "0x00". This cause the unmatched dimension size error while I tried to assign the char array to a variable (with a dimension of the string length). I donot understand how the "0x00" relates to the missing value of original string (as detailed on the ncl webpage of the stringtochar function). And how can I avoid it? Can someone clarify for me. Thanks!
> > Xin
> > _______________________________________________
> > ncl-talk mailing list
> > List instructions, subscriber options, unsubscribe:
> > http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Jul 9 10:20:05 2012

This archive was generated by hypermail 2.1.8 : Mon Jul 09 2012 - 10:45:32 MDT