实数与字符串之间怎么转化

描述

有些朋友经常问我实数与字符串之间怎么转化,尤其是在EthernetKRL的Binarystream的使用中。那么这边文章以代码的形式来展示。

一.实数转化为字符串:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
;***************************************************;* Customer     :                                  *;* Roboter      :                                  *;* Version      : Vxxxxxx                          *;* Roboter Nr.  : xxxxxx                           *;* Controller Nr: xxxxxx                           *;*                                                 *;* Autor        : Andrew Wang                      *;* Company      :                                  *;* Department   :                                  *;* Telephone    : 86 156-8082-2827                 *;*                                                 *;* Version      : 1.0                              *;* Created      : 12.12.2018                       *;* Modified     :                                  *;* Project      :                                  *;*                                                 *;* Program Name : Real_2_String                   *;* Convert a REAL variant to a string variable;    *;************************************************** DEFFCT CHAR[32] Real_2_String(rVar:IN )DECL CHAR Ret[32]DECL INT Offset,IDECL STATE_T stateDECL REAL rVarOffset=0FOR I=1 TO 32  Ret[I]=0ENDFORSWRITE(Ret[],State,offset,"%f",rVar)Return (ret[])ENDFCT

测试程序:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
DEF CHeck( )DECL CHAR test[32]DECL REAL xDECL INT I FOR I=1 TO 32 Test[i]=0ENDFORX=-3.14Test[]=Real_2_string(x)HALTEND

 

测试结果:

字符串

如果将程序修改下,就可以保留小数点后几位的格式:

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
;***************************************************;* Customer     :                                  *;* Roboter      :                                  *;* Version      : Vxxxxxx                          *;* Roboter Nr.  : xxxxxx                           *;* Controller Nr: xxxxxx                           *;*                                                 *;* Autor        : Andrew Wang                      *;* Company      :                                  *;* Department   :                                  *;* Telephone    : 86 156-8082-2827                 *;*                                                 *;* Version      : 1.0                              *;* Created      : 12.12.2018                       *;* Modified     :                                  *;* Project      :                                  *;*                                                 *;* Program Name : Real_2_String                   *;* Convert a REAL variant to a string variable;    *;************************************************** DEFFCT CHAR[32] Real_2_String(rVar:IN )DECL CHAR Ret[32]DECL INT Offset,IDECL STATE_T stateDECL REAL rVarOffset=0FOR I=1 TO 32  Ret[I]=0ENDFORSWRITE(Ret[],State,offset,"%.3f",rVar)Return (ret[])ENDFCT

测试结果:

字符串

二.字符串转实数:

   对于字符串转实数有两种方式,一种是系统函数,一种用户自定义的函数;

  1. 系统自定义函数:

    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    ;**************************************************;* System Built-in function-StrToReal             *;* Return Vaule=StrToReal(CHAR:IN,REAL:OUT) ;     *;* Convert a string in a Real variable   ;        *;* Range value of Argument IN :Char Array ;       * ;* Range value of Argument OUT :REAL ;            * ;* Range value of Result is Bool as Result;       *;**************************************************

    测试程序:        

    测试结果:

    字符串

  2. 用户定义函数

    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    ;***************************************************;* Customer     :                                  *;* Roboter      :                                  *;* Version      : Vxxxxxx                          *;* Roboter Nr.  : xxxxxx                           *;* Controller Nr: xxxxxx                           *;*                                                 *;* Autor        : Andrew Wang                      *;* Company      :                                  *;* Department   :                                  *;* Telephone    : 86 156-8082-2827                 *;*                                                 *;* Version      : 1.0                              *;* Created      : 12.12.2018                       *;* Modified     :                                  *;* Project      :                                  *;*                                                 *;* Program Name : String_To_Real                   *;* Convert a string variant to a real  variable;   *;************************************************** GLOBAL DEFFCT REAL String_To_Real(StringIn[]:IN)DECL STATE_T state DECL CHAR StringIn[]DECL REAL RealOutDECL INT zOffsetzOffset=0SREAD(StringIN[],State,zOffset,"%f",RealOut)RETURN(RealOut)ENDFCT

    测试程序:

     

     

    字符串

    测试结果:

    字符串

 

责任编辑:xj

原文标题:库卡编程篇实数与字符串转化

文章出处:【微信公众号:机器人及PLC自动化应用】欢迎添加关注!文章转载请注明出处。


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分