Re: hexadecimal from byte?

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu, 17 Jan 2008 19:36:12 -0700

Andrea, you are asking for binary representation, not hex. E.g.
base 2, not base 16.

If you are lucky then a sprinti call similar to David Brown's
suggestion might work:

   print(sprinti("%0.8b",123))

Note the special code "b" for binary display format. Unfortunately
this is platform dependent on the local system sprintf function
(outside NCL), and the "b" extension is often not supported.

Otherwise, just use this, which makes use of integer truncation:

   ival = 123
   nbits = 10 ; number of bits to display
   str1 = "" ; bit accumulator

   do i = 1, nbits
     str1 = (ival % 2) + str1 ; pick off the lowest bit
     ival = ival / 2 ; shift right one bit, drop lowest
   end do

   print (str1)

Note this will not work for negative numbers without a little fiddling.

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/

Andrea Hahmann wrote:
> Hi Adam
>
> What you suggests just gives be the number 2 as a string "2". What I
> need is the hexadecimal representation of the number 2. For example,
> the number 123 is "01111011". In my case each of these "0/1" represent
> when a given test has been passed or not. That is the information I
> need. Any suggestions?
>
> Thanks
> Andrea
>
> asphilli_at_ucar.edu wrote:
>> Hi Andrea,
>>
>> The conversion is done automatically behind-the-scenes in most cases. For
>> instance:
>>
>> a = 2
>> b = "Quantity needed: "+a
>> printVarSummary(b)
>> print(b)
>>
>> or
>> a = 2
>> b = "Hello"
>> b = a
>> print(b)
>>
>> Adam
>>
>>
>>> Reading the documentation, I have found that I can convert an
>>> hexadecimal number to integer using the function "stringtointeger". Is
>>> there a function to do the conversion back? If not, perhaps somebody
>>> has already written such a thing?
>>>
>>> Thanks,
>>> Andrea
>>>
>>> --
>>> ----------------------------------------------------------------
>>> Andrea N. Hahmann, Ph.D.
>>> Research Applications Laboratory
>>> Natl. Center for Atmospheric Research Phone: 1-303-497-8383
>>> PO BOX 3000 Fax: 1-303-497-8401
>>> Boulder, CO 80301 hahmann_at_ucar.edu
>>> ----------------------------------------------------------------
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk_at_ucar.edu
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk_at_ucar.edu
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Jan 17 2008 - 19:36:12 MST

This archive was generated by hypermail 2.2.0 : Wed Jan 23 2008 - 14:19:33 MST