# Compose string

**URL:** <https://forum.sierrawireless.com/t/compose-string/1403>\
**Category:** Open AT\
**Created:** [June 17, 2007, 7:14pm UTC](https://forum.sierrawireless.com/t/compose-string/1403 "2007-06-17T19:14:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![usa](https://avatars.discourse-cdn.com/v4/letter/u/8baadc/32.png) [@usa](https://forum.sierrawireless.com/u/usa)\
**Post date:** [June 17, 2007, 7:14pm UTC](https://forum.sierrawireless.com/t/compose-string/1403/1 "2007-06-17T19:14:25Z")

</div>

Send data via WIP plugin to server.

```auto
ascii response[255];

u8 data[6];

// fill sample date value
data[0] = 0x01;
data[1] = 0x02;
data[2] = 0x03;
data[3] = 0x04;
data[4] = 0x05;
data[5] = 0x06;

wm_sprintf(response, "%s%s%s", "rq", data, "\n");
wip_write(socket, response, wm_strlen(response));
```

server recieved: “rq\n”  
\n is are 0x13 char

where my data?

---

<div class="post-metadata">

**Author:** ![jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jan](https://forum.sierrawireless.com/u/jan)\
**Post date:** [June 17, 2007, 7:29pm UTC](https://forum.sierrawireless.com/t/compose-string/1403/2 "2007-06-17T19:29:30Z")

</div>

Hi,

where is your string end in data? strings need to be terminated with a null character in C, and this is required for use of %s. Your lucky that the program didn’t crash, because it could very well be that the memory contents after data doesn’t contain a 0x00 character for a length greater than 255 characters, so you would copy into the RAM after response variable and overwriting critical parts.

Also, I would suggest not to use wm\_sprintf() just to concatenate strings. Use wm\_strcpy() and wm\_strcat() instead! I never had problems with that, where wm\_sprintf() caused some trouble to me before.

Best Regards,  
Jan

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [June 18, 2007, 1:44pm UTC](https://forum.sierrawireless.com/t/compose-string/1403/3 "2007-06-18T13:44:24Z")

</div>

Not only that… the “data” that is being sent are all non-printable characters.

If you look at the actual data you receive, you may see those additional bytes but they will not be printed as they are now.

---

<div class="post-metadata">

**Author:** ![usa](https://avatars.discourse-cdn.com/v4/letter/u/8baadc/32.png) [@usa](https://forum.sierrawireless.com/u/usa)\
**Post date:** [June 19, 2007, 7:50pm UTC](https://forum.sierrawireless.com/t/compose-string/1403/4 "2007-06-19T19:50:38Z")

</div>

Help again:

```auto
ascii response[255];

u8 data[6];

// fill sample date value 
data[0] = 0x01; 
data[1] = 0x02; 
data[2] = 0x03; 
data[3] = 0x04; 
data[4] = 0x05; 
data[5] = 0x06;

wm_strcpy(response, "rq");  
wm_strcat(response, data);
wm_strcat(response, "\n");
wip_write(socket, response, wm_strlen(response));
```

server recieved “0x48 0x47 0xffffffbc 0x0a”  
but where by data array?

---

<div class="post-metadata">

**Author:** ![milan\_va](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@milan\_va](https://forum.sierrawireless.com/u/milan_va)\
**Post date:** [June 19, 2007, 10:55pm UTC](https://forum.sierrawireless.com/t/compose-string/1403/5 "2007-06-19T22:55:00Z")

</div>

You should start by adding a zero at the end of data, so that the string is properly terminated.

```auto
ascii response[255];

u8 data[6+1];

// fill sample date value 
data[0] = 0x01; 
data[1] = 0x02; 
data[2] = 0x03; 
data[3] = 0x04; 
data[4] = 0x05; 
data[5] = 0x06;
data[6] = 0;

wm_strcpy(response, "rq");  
wm_strcat(response, data);
wm_strcat(response, "\n");
wip_write(socket, response, wm_strlen(response));
```

> [@usa](#):
>
> server recieved “0x48 0x47 0xffffffbc 0x0a”

That’s really strange. Can you send a copy of response to UART1 or check it with TRACE() ? How about Debug mode, can you check if the string is built correctly?  
How about avoiding wm\_strcat():

```auto
ascii *response;

response="rq\x01\x02\x03\x04\x05\x06\n";
wip_write(socket, response, wm_strlen(response));
```

If you want to fix the problem, you need first to find exactly the point where it is: the first wm\_strcat(), or the second wm\_strcat(), or the WIP library, or the receiving server.

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.sierrawireless.com/u/tobias)\
**Post date:** [June 20, 2007, 6:21am UTC](https://forum.sierrawireless.com/t/compose-string/1403/6 "2007-06-20T06:21:06Z")

</div>

> [@](#):
>
> server recieved “0x48 0x47 0xffffffbc 0x0a”

I don’t believe for a moment that that is exactly what the server is receiving byte for byte.  
Are you sure that your server side program is actually written correctly when it gives you this output?

---

<div class="post-metadata">

**Author:** ![jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jan](https://forum.sierrawireless.com/u/jan)\
**Post date:** [June 20, 2007, 7:11am UTC](https://forum.sierrawireless.com/t/compose-string/1403/7 "2007-06-20T07:11:18Z")

</div>

Hi usa,

> [@usa](#):
>
> ```auto
> ascii response[255];
> u8 data[6];
> ```

where are these variable declarations in the code? are those local variables in a function or global variables?

Could you try to make them static like this

```auto
static ascii response[255];
static u8 data[6];
```

and see what happens?

Best Regards,  
Jan

---

<div class="post-metadata">

**Author:** ![usa](https://avatars.discourse-cdn.com/v4/letter/u/8baadc/32.png) [@usa](https://forum.sierrawireless.com/u/usa)\
**Post date:** [June 20, 2007, 10:09am UTC](https://forum.sierrawireless.com/t/compose-string/1403/8 "2007-06-20T10:09:01Z")

</div>

> [@jan](#):
>
> where are these variable declarations in the code? are those local variables in a function or global variables?

Response is global static variable, data is local variable.

2milan\_va thanks, will try.
