[英]Passing a Float into a String in C
How do I add a variable into a string?如何将变量添加到字符串中? I want to transmit the string with the actual value of
depth
.我想用
depth
的实际值传输字符串。 So how do I add this into the message?那么如何将其添加到消息中呢?
float depth = ((pressure) / (g)) * 100;
char message[] = "$DBS,,f,depth,M,,,F*hh<CR><LF>"; //Put actual depth here
I would suggest to use snprintf()
the usage is similar to printf()
and the suggested sprintf()
, but you have a parameter for max length of the string.我建议使用
snprintf()
用法类似于printf()
和建议的sprintf()
,但您有一个字符串最大长度的参数。
This way you can avoid buffer overflows.这样您就可以避免缓冲区溢出。
depth = ((pressure)/(g))*100;
char message[100];
snprintf(message, sizeof message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );
if the string would be larger then the array you can accomodate for this by checking the return value如果字符串会更大,那么您可以通过检查返回值来适应这个数组
int length = snprintf(message, sizeof message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );
if (length > sizeof message)
{
//do something to handle if neccessary
}
In the linked article you can find a list of specifiers and modifiers that can be used to modify the format of the float in your string, which can be very useful.在链接的文章中,您可以找到可用于修改字符串中浮点数格式的说明符和修饰符列表,这非常有用。 Depending on how big your number is you might want to use %e (for decimal exponent notation or often times called engineering notation).
根据您的数字有多大,您可能想要使用 %e(用于十进制指数表示法或通常称为工程表示法)。 Additionally you can influence how many digits are used and other things.
此外,您可以影响使用的数字数量和其他内容。 It is really worth to check out.
真的很值得一看。
Alternativly you can check out http://www.cplusplus.com/reference/cstdio/snprintf/ and http://www.cplusplus.com/reference/cstdio/printf/ which is easier to understand for some people, but not that in detail.或者,您可以查看http://www.cplusplus.com/reference/cstdio/snprintf/和http://www.cplusplus.com/reference/cstdio/printf/这对某些人来说更容易理解,但并非如此详细。
Try the sprintf()
:试试
sprintf()
:
char message[100];
sprintf(message, "$DBS,,f,%f,M,,,F*hh<CR><LF>", depth);
That %f
in the string is where the depth value will go.字符串中的
%f
是深度值所在的位置。
Use sprintf()
.使用
sprintf()
。 Similar to printf()
but it prints within a string.与
printf()
类似,但它在字符串中打印。
Of course the target buffer must be big enough to contain the whole string:当然,目标缓冲区必须足够大以包含整个字符串:
char message[100];
float depth = ((pressure)/(g))*100;
sprintf(message, "$DBS,,f,%f,M,,,F*hh\r\n", depth );
So %f
is the conversion character that will be substituted by depth value in the final string.所以
%f
是将被最终字符串中的深度值替换的转换字符。 In order to define the number of decimals you can use the %0.Nf
format, that will show your value with a N decimals precision.为了定义小数位数,您可以使用
%0.Nf
格式,它将以N小数精度显示您的值。
How do I add a variable into a string?
如何将变量添加到字符串中?
Use sprintf()
or better snprintf()
.使用
sprintf()
或更好的snprintf()
。
I want to transmit the string with the actual value of depth.
我想用深度的实际值传输字符串。 So how do I add this into the message?
那么如何将其添加到消息中呢?
A problem not yet answered is how to convey all the information of float depth
to the recipient.一个尚未解决的问题是如何将
float depth
所有信息传达给接收者。 This gets into what format should be used.这进入了应该使用什么格式。
"%.*e"
, "%.*g"
and "%a"
are 3 good choices. "%.*e"
、 "%.*g"
和"%a"
是 3 个不错的选择。 "%f"
is a weak choice as it provides no info on small values and excessive digits on large values. "%f"
是一个弱选择,因为它不提供关于小值和大值过多数字的信息。 See Printf width specifier to maintain precision of floating-point value请参阅Printf 宽度说明符以保持浮点值的精度
Suggest:建议:
// -d. dddddddd e-ddd\0
#define STR_FLT_N (3 + (FLT_DECIMAL_DIG -1) + 6)
#define FMT "$DBS,,f,%.*e,M,,,F*hh<CR><LF>"
#define BUF_N (sizeof(format) + STR_FLT_N)
char message[BUF_N];
snprintf(message, sizeof message, FMT, FLT_DECIMAL_DIG-1, depth);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.