2

How do I add a variable into a string? I want to transmit the string with the actual value of 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
Kami Kaze
  • 2,069
  • 15
  • 27
Finners
  • 37
  • 7

4 Answers4

7

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.

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). 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.

Kami Kaze
  • 2,069
  • 15
  • 27
2

Try the 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.

DarkAtom
  • 2,589
  • 1
  • 11
  • 27
  • 2
    I'd define the number of decimal digits with format specifier. `snprintf` always better than `sprintf` – LPs Dec 10 '19 at 16:12
1

Use sprintf(). Similar to printf() but it prints within a string.

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. 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.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • 1
    Better explanation in respect to the other answer but the use of sprintf is wrong as the buffer is missing. – Kami Kaze Dec 10 '19 at 16:19
1

How do I add a variable into a string?

Use sprintf() or better 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. This gets into what format should be used.

"%.*e", "%.*g" and "%a" are 3 good choices. "%f" is a weak choice as it provides no info on small values and excessive digits on large values. See Printf width specifier to maintain precision of floating-point value

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);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256