搜索此博客

2017年10月9日星期一

『转载』IDL二进制与十进制互相转化代码

;-----------------------二进制到十进制-----------------
;主程序
FUNCTION f_b2d,n
  ;By friendchj
  ;Base on GORDON NANA KWESI AMOAKO's MATLAB Program
  ;function f_b2d converts numbers with binary fractions
  ;or binary fractions only to decimalnumbers
  ;------------------------------------------------------
  ;INPUT: n, n is a String of Binary numbers e.g. f_b2d('11001.101') gives us
  ;OUTPUT: A Decimal Number
  ;SAMPLE input and output
  ;f_b2d('11001.101')=25.6250
  ;f_b2d('110000111.111111101')=391.9941
  ;f_b2d('0.11001111101')= 0.8110
  ;Converting the Number to String
  n=strtrim(n,2)
  numadd=0
  p=strlen(n)
  ;-------------------------------------------------------
  if strpos(n,'.') EQ -1 then begin
    y=bin2dec(n)
  endif else begin
    ;----------------------------------------------------------------------
    j=strpos(n,'.')
    i_part=strmid(n,0,j)
    f_part=strmid(n,j,p-j)
    for i=0,strlen(f_part)-1 do begin
      if strmid(f_part,i,1) EQ '1' then numadd=numadd+(0.5^i)
    endfor
    y=f_b2d(i_part)+numadd
    ;----------------------------------------------------------------------
  endelse
  return,y
end
;------------------------------------------------------------------------
;子程序
FUNCTION bin2dec,n
  ; By friendchj
  p=strlen(n)
  y=0L
  for i=0,p-1 do begin
    y=y+strmid(n,i,1)*2^(p-i-1)
  endfor
  return,y
  ;-------------------十进制到二进制的装换-----------------------------------------
  ;主程序
FUNCTION f_d2b,n
  ;-----------------------------------------------
  ;By friendchj
  ;Based on GORDON NANA KWESI AMOAKO's MATLAB Program
  ;function f_d2b converts numbers with fractions or
  ;fractions only to BINARY
  ;-----------------------------------------------
  ;INPUT: n e.g. 25.625
  ;OUTPUT: Binary number e.g. 11001.101
  ;Converting the Number to String

  ;将数字转化为字符串
  strn=num2str(n)
  p=strlen(strn)
  ;------------------------------------------------
  if strpos(strn,'.') EQ -1 then begin
    ;将整数数值转化为二进制
    y=d2b(n)
    return,y
  endif else begin
    ;------------------------------------------------
    k=strpos(strn,'.')

    ;Retrieving INTEGER and FRACTIONAL PARTS as strings
    i_part=strmid(strn,0,k)
    f_part=strmid(strn,k,p-k)

    ;Converting the strings back to numbers
    ni_part=long(i_part)
    nf_part=float(f_part)
    ni_part=d2b(ni_part)
    strtemp=''
    temp=nf_part
    ;-------------------------------------------------
    t='1'
    s='0'
    while nf_part GE 0 do begin
      nf_part=nf_part*2
      if (nf_part EQ 1) or (nf_part EQ temp) then begin
        strtemp=string(strtemp,t)
        strtemp=STRCOMPRESS(strtemp, /REMOVE_ALL)
        break
      endif else begin
        if nf_part GT 1 then begin
          strtemp=string(strtemp,t)
          strtemp=STRCOMPRESS(strtemp, /REMOVE_ALL)
          nf_part=nf_part-1
        endif else begin
          strtemp=string(strtemp,s)
          strtemp=STRCOMPRESS(strtemp, /REMOVE_ALL)
        endelse
      endelse
    endwhile
    if ni_part EQ 0 then begin
      y=string('0.',strtemp)
      y=STRCOMPRESS(y, /REMOVE_ALL)
      return,y
    endif else begin
      y=string(ni_part,'.',strtemp)
      y=STRCOMPRESS(y, /REMOVE_ALL)
      return,y
    endelse
    ;------------------------------------------------
  endelse
end
;----------------------------------------------------
;子程序
;+-------------------------------------------------------------------------------
;| converting float/double type numer into its original inputform
;| double type has appoximately 16 digits of significance. float has 7
;|
;| in : a : the data to convert
;|    : Keyword : tip: [1,0] : [output tps(E:D) or not]
;| out: Return, String of the data
;| Example:
;|        a = 4356245.1245D ; 注意要确定数据类型为Double! 如果是浮点型只能达到7位精度
;|    print, dbl2str(a) ;
;+-------------------------------------------------------------------------------
Function dbl2str, a
  Compile_Opt Strictarr
  tp=size(a,/type)
  if tp ne 4 and tp ne 5 then begin
    a=double(a)
    tp=5
  endif
  tps=tp eq 4?'E':'D'
  rawstr=strtrim(string(a,format='(g)'),2);full width G format
  pos = Strpos(rawstr, 'e', /REVERSE_SEARCH) ; 有e,不砍0
  if pos ne -1 then Return, rawstr
  ;
  ; 砍0
  no0 = 1
  while no0 do begin
    len = Strlen(rawstr)
    endstr = Strmid(rawstr, len-1, 1)
    if endstr ne '0' then no0 = 0 $
    else begin
      rawstr = Strmid(rawstr, 0, len-1)
    endelse
  endwhile
  ;
  ;
  Return, rawstr
End
;+-------------------------------------------------------------------------------
;| 将数值型转成字符串
;| 注意,不能处理数值数组、复数
;| 输入: 数值型变量
;| 输出: 对应的字符串
;| 示例:
;|      a = 4356245.1245D
;|      stra = NumToStr(a)
;| 作者: Huxz 2007-10
;+-------------------------------------------------------------------------------
Function Num2Str, num
  Compile_Opt Strictarr
  num = num[0]
  data_type = Size(num, /type)
  case data_type of
    1: Return, Strtrim(Fix(num), 2) ; Byte
    2: Return, Strtrim(num, 2); Int
    3: Return, Strtrim(num, 2); Long
    4: Return, Dbl2str(num); Float
    5: Return, Dbl2str(num); Double
    12: Return, Strtrim(num, 2); Unsigned Int
    13: Return, Strtrim(num, 2); Unsigned Long
    14: Return, Strtrim(num, 2); Long64
    15: Return, Strtrim(num, 2); Unsigned Long64
    else: Return, num
  endcase
End
;-----------------------------------------------------
;子程序
FUNCTION d2b,n
  strtemp=''
  if n lt 0 then begin
    print,' %f is not a valid number\n'
  endif else begin
    while n NE 0 do begin
      strtemp=string(string((n mod 2)),strtemp)
      strtemp=STRCOMPRESS(strtemp, /REMOVE_ALL)
      n=floor(n/2.0)
    endwhile
  endelse
  y=strtemp
  return,y
end

没有评论:

发表评论

LibSVM Chinese Brief Infroduction

Reference: [1]  https://blog.csdn.net/v_july_v/article/details/7624837 [2]  https://wenku.baidu.com/view/c402e983336c1eb91b375d37.html?fr...

  • Word (2)