Octal conversion with stringtoint

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu, 17 Jul 2008 20:20:10 -0600

NCL support,

There is a problem with the "stringtoint" family of type converters.
  The documentation says that a string of digits with a leading zero
will be interpreted in octal. However, I seem to be getting decimal
conversion rather than octal on my system, Mac OS 10.5 on PPC, NCL
versions 4.2 through 5.0.1 (pre-release).

These functions all act the same way:

   stringtoint
   stringtointeger
   stringtoshort
   stringtolong

Please see the attached demo program, which incorporates parts of
Example 3 on each function's documentation page. For octal
conversion I am getting the integers 5, 100, 222 instead of the
expected values 5, 64, 146.

I wonder whether this is widespread on other platforms, and how much
existing user code is silently depending on the actual behavior with
decimal data that includes leading zeros. Thanks for your
consideration.

Dave Allured
CU/CIRES Climate Diagnostics Center (CDC)
http://cires.colorado.edu/science/centers/cdc/
NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
http://www.cdc.noaa.gov/

;---------------------------------------------------------------------
;
; Test for string conversion functions with octal representation.
;
; Downloaded 2008-jul-17
; From NCL website:
; http://www.ncl.ucar.edu/Document/Functions/Built-in/stringtoint.shtml
;
; Modifications:
;
; 1. Tests for other stringto... functions were added.
; 2. Output format was modified for clarity.
;
;----------------------------------------------------------------------

; Example 3
; Shows how octal and hexadecimal string representations are handled.

begin

; Specify an array of strings representing octal numbers

  a = (/"05", "0100", "0222"/)

  print ("")
  print ("Octal conversion, stringtoint, expected = (/ 5, 64, 146 /)")

  b = stringtoint(a)
  print(b+"")

;-----------------------------

; Specify an array of strings representing hexadecimal numbers

  c = (/"0x5", "0x10", "0x9A"/)

  print ("")
  print ("Hexadecimal conversion, stringtoint, expected = (/ 5, 16, 154 /)")

  d = stringtoint(c)
  print(d+"")

;-----------------------------

  print ("")
  print ("Octal conversion, stringtointeger, expected = (/ 5, 64, 146 /)")

  b2 = stringtointeger(a)
  print(b2+"")

;-----------------------------

  print ("")
  print ("Octal conversion, stringtoshort, expected = (/ 5, 64, 146 /)")

  b3 = stringtoshort(a)
  print(b3+"")

;-----------------------------

  print ("")
  print ("Octal conversion, stringtolong, expected = (/ 5, 64, 146 /)")

  b4 = stringtolong(a)
  print(b4+"")
end

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jul 17 2008 - 20:20:10 MDT

This archive was generated by hypermail 2.2.0 : Mon Jul 21 2008 - 14:41:34 MDT