Re: convert an array from decimal to binary

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Tue Apr 23 2013 - 10:28:15 MDT

You could try using "getbitsone" in conjunction with "str_concat" and
"toint".

http://www.ncl.ucar.edu/Document/Functions/Built-in/str_concat.shtml
http://www.ncl.ucar.edu/Document/Functions/Built-in/getbitsone.shtml

x = 7
i = getbitsone(x) ; 1x32 integer array
bin = toint(str_concat(tostring(i(0,:))))

print(bin)

On Tue, 23 Apr 2013 10:22:05 -0500
  Zhifeng Yang <yangzf01@gmail.com> wrote:
> Hi all
>
> Is there some function that can convert an array from decimal to
>binary? I
> have tried the following code. However it can only be used for one
>digit at
> one time. I want to convert one array to binary.
>
> function dectobin(dec_num:integer)
> local dq, cmd
> begin
> ;---Double quote character
> dq = str_get_dq()
>
> ;---UNIX command that converts decimal to binary
> cmd = "echo " + dq + "ibase=10;obase=2;" + dq + dec_num + " | bc "
>
> ;---Execute UNIX command and return an integer
> return(toint(systemfunc(cmd)))
> end
>
> ; Main code
> ;---Decimal number to convert
> begin
> dec_num = 7
> bin_num = dectobin(dec_num)
>
> print("decimal number = " + dec_num)
> print("binary number = " + bin_num)
> end
> So do you have any other ideas on this?
> Thank you
> Bruce
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Apr 23 10:28:22 2013

This archive was generated by hypermail 2.1.8 : Tue Apr 23 2013 - 12:54:13 MDT