# Re: convert a decimal to 32 bit unsigned binary

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed Feb 20 2013 - 13:55:10 MST

a = str_index_of_substr(str2,"1",0)
print(dimsizes(a))

(a will be an array containing the index of each "1" character; the size of the array is how many "1"s there are.
-dave

On Feb 20, 2013, at 1:45 PM, cheryl Ma wrote:

> One more question: Now suppose I have str2 =00100100000000000000000000000000,
>
> using ncl, how to count how many days having the observations (1 means having observations, 0 means no)?
>
> Thanks,
> Xiaoyan
>
>
>
> On Tue, Feb 19, 2013 at 1:52 PM, Dave Allured - NOAA Affiliate <dave.allured@noaa.gov> wrote:
> Xiaoyan,
>
> Okay, here is an NCL program that converts FROM a 32-bit unsigned integer, TO a character string of length 32. The output should be equivalent to the Fortran program.
>
> begin
> nbits = 32 ; number of bits to display
>
> print ("i = " + i + " (NCL type = " + typeof (i) + ")")
>
> str1 = ""
> str2 = ""
> ic = i
>
> do n = 1, nbits
> bit = (ic % 2) ; pick off the lowest bit
> ic = ic / 2 ; shift right one bit, drop lowest
> str1 = bit + str1
> str2 = str2 + bit
> end do
>
> print ("MSB first: " + str1)
> print ("LSB first: " + str2)
> end
>
> Output:
> (0) i = 36 (NCL type = uint)
> (0) MSB first: 00000000000000000000000000100100
> (0) LSB first: 00100100000000000000000000000000
>
> --Dave
>
> On Tue, Feb 19, 2013 at 10:27 AM, cheryl Ma <xiaoyancloud@gmail.com> wrote:
> Thanks, Dave.
>
> Below is a fortran code which is provided by Andrew Mai (Thanks Andrew !). cv in the code is what I want.
>
> program dumpbits
> implicit none
>
> integer :: i,ic,icv
> character(len=32) :: c,cv
>
> i = 36
> print '(b32.32)',i
>
> write(c,'(b32.32)') i
>
> do ic=1,32
> icv=33-ic
> cv(icv:icv)=c(ic:ic)
> enddo
>
> print '(a32)',cv
>
> end
>
>
>
> On Tue, Feb 19, 2013 at 11:54 AM, Dave Allured - NOAA Affiliate <dave.allured@noaa.gov> wrote:
> Xiaoyan,
>
> Oops, my mistake. I thought that the digits of "36" meant day numbers 3 and 6. By coincidence, it really is the encoded value of the 3rd and 6th bits, in other words 36 = 2^(3-1) + 2^(6-1). Sorry for the confusion.
>
> Returning to your original question, use the NCL function "touint" to convert other NCL data types (numbers and strings) to NCL 32-bit unsigned binary. The following example converts "36" as an NCL string type to data type "uint" which is NCL 32-bit unsigned binary. Is this what you are looking for?
>
> x = "36" ; NCL string type
> y = touint (x)
> print (y)
>
> Output:
> Variable: y
> Type: uint
> Total Size: 4 bytes
> 1 values
> Number of Dimensions: 1
> Dimensions and sizes: [1]
> Coordinates:
> (0) 36
>
> "uint" and other NCL data types are explained on this page:
> http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclDataTypes.shtml
>
> If this is not what you want, it might be helpful if you show us the Fortran code that you mentioned, so that we can understand what data structure you are trying to get.
>
> --Dave
>
> On Tue, Feb 19, 2013 at 7:26 AM, cheryl Ma <xiaoyancloud@gmail.com> wrote:
> Dave,
>
> in this case, "36" is a decimal number. It need be converted to a 32-bit binary number and then one can know which day having the observation, e.g. If there is observation on the 1st day of the month, then bit 1 would be set to true and if there is observation on the 2nd day of the month, then bit 2 would be set to true and so on.
>
> We can write a fortran code to convert a decimal number to a 32-bit binary number. My question is if there is a function in ncl to do this?
>
> Thanks,
> Xiaoyan
>
>
>
>
> On Mon, Feb 18, 2013 at 4:01 PM, Dave Allured - NOAA Affiliate <dave.allured@noaa.gov> wrote:
> Xiaoyan,
>
> Be careful. In the example you show, "36" is NOT a decimal number. It looks like it is really a list of two separate integers, 3 and 6.
>
> It is good that the example image showed the graphic mapping between calendar days and bit positions, because the statement "Days of Month Observed = 36" is badly constructed and very misleading. Maybe they should have said "Days of Month Observed = 3 and 6".
>
> So, what do you really want to do? Maybe convert a LIST of calendar day numbers 1 to 31, to a 32-bit unsigned integer, using bit position encoding as shown in this example?
>
>
> --Dave
>
> On Mon, Feb 18, 2013 at 8:57 AM, cheryl Ma <xiaoyancloud@gmail.com> wrote:
> Dear all,
>
> Is there any function in NCL to convert a decimal number to a 32-bit unsigned binary? In an example shown below, 36 is a decimal, so its binary is 00100100000000000000000000000000.
>
> Thanks,
> Xiaoyan
>
> ==============================================
>
>
> _______________________________________________
> 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
>
>
>
>
>
> _______________________________________________
> 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 Wed Feb 20 13:55:25 2013

This archive was generated by hypermail 2.1.8 : Thu Feb 21 2013 - 11:26:43 MST