arduino:int & double 转string 适合12864下使用

简介: 转自:http://www.geek-workshop.com/forum.php?mod=viewthread&tid=3383&highlight=12864 很多人在玩12864的时候,都会发现不能直接显示字符,因为大多数12864类库没有显示数值的函数,那么我们就需要把int型变量转换成字...

转自:http://www.geek-workshop.com/forum.php?mod=viewthread&tid=3383&highlight=12864

很多人在玩12864的时候,都会发现不能直接显示字符,因为大多数12864类库没有显示数值的函数,那么我们就需要把int型变量转换成字符串,方法很简单,只要在代码末尾加上一个功能函数即可~

 

    char* itostr(char *str, int i)
    {
        sprintf(str, "%d", i); return str; }

 

把上述代码放入程序末尾,在程序头定义一个char a[25],在读取完数值之后就可以轻松的用一行itostr(a,b);来转换,其中a是之前定义的char,b是数值变量,是不是很方便呢?

======================================
附一个double转string的.

 

    void setup() {
      // put your setup code here, to run once:
      double test = 1.23; char test2[25] ; dtostr(test2,test); } void loop() { // put your main code here, to run repeatedly:  } char* dtostr(char *str, double d) { sprintf(str, "%f", d); return str; }

 

目录
打赏
0
0
0
0
13
分享
相关文章
Java中String强转int:一种常见的错误和解决方法
在Java中将非数字字符串转换为整数会导致`NumberFormatException`。要解决这个问题,可以使用`try-catch`捕获异常,正则表达式验证数字格式,或利用异常信息提供错误提示。例如,`Integer.parseInt()`会因遇到非数字字符如`"123abc"`而抛出异常,但通过异常处理或正则`\\d+`可确保安全转换。记得在编程时避免直接强转,以防止程序异常中断。
|
5月前
|
使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小
【10月更文挑战第13天】使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。
147 1
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
Dart或Flutter中解决异常-type ‘int‘ is not a subtype of type ‘double‘
216 4
|
7月前
|
Dart基础:进制转换、int与string互转
Dart基础:进制转换、int与string互转
179 3
|
8月前
|
Java中将保留四位小数的Double转换为String的方法详解
选择合适的方法,可以使代码更加简洁、高效,同时也能满足不同场景下的需求。
184 5
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
遍历字符串,String line = xxx for(int i = 0;i<line.length();i++){system.out.println(line.chartAt(i)); 单个
|
10月前
|
计算 int, float, double 和 char 字节大小
计算 int, float, double 和 char 字节大小。
105 3
|
10月前
channelSftp.put(InputStream src, String dst, int mode);里的mode都是什么类型的
【5月更文挑战第15天】channelSftp.put(InputStream src, String dst, int mode);里的mode都是什么类型的
302 2
|
9月前
|
String转化为Int
String转化为Int
|
10月前
int 和 String 互相转换的多种方法
int 和 String 互相转换的多种方法
89 1
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等