【问题标题】:Using of variable instead of hard-coded string使用变量而不是硬编码字符串
【发布时间】:2018-06-14 08:18:02
【问题描述】:

我打算使用含TM1637的led显示屏。
编程语言:avr-gcc
我找到了这个库:
https://github.com/project37cat/TM1637-lib-AVR/blob/master/tm1637.h
在这个例子中使用函数 led_print 它对我有用: https://github.com/project37cat/TM1637-lib-AVR/blob/master/main.c
但是我不知道如何为它使用变量而不是硬编码字符串(计划使用整数值,但我不确定它是否可能,因为库需要字符串)。

【问题讨论】:

    标签: avr atmega


    【解决方案1】:

    编程语言:avr-gcc

    GCC 是一种跨平台编译器,而不是一种编程语言。在这种情况下,您正在使用“C”进行编程,但它也可以是 C++。

    计划使用整数值,但我不确定它是否可能,因为库需要字符串

    您必须将所需的整数值转换为字符串(字符数组)表示。有几种方法可以实现这一目标。一个常见的方法是使用snprintf()。也可以看看format strings

    #include <stdio.h>   // library that contains snprintf()
    
    char buffer[10];   // this is the char-buffer where your "string" will be stored
    int value = 234452;   // this is the value you want to convert
    snprintf(buffer, 10, "%d", value);  // this coverts "value" to a decimal representation as specified by the format string "%d" and copies it to "buffer"
    

    那你应该可以用了

    led_print(0, buffer);
    

    【讨论】:

    • 谢谢,完美解决了我的问题!也感谢您指出语言/编译器之间的区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多