Comments (9)
Hello @ritazh
Can you please attach a minimal repro? Thank you. On a side note, if you are using a byte array, then it will not be null terminated.
Best Regards,
Andrei Porumb
from azure-iot-sdk-c.
@anporumb I used this mqtt sample. To reproduce, output the buffer returned by IoTHubMessage_GetByteArray
here, like this:
(void)printf("IoTHubMessage_GetByteArray buffer sample: \"%s\" \n", buffer);
you will see the extra characters at the end of the buffer.
from azure-iot-sdk-c.
Hello @ritazh ,
I beleive the way you are printing the message is wrong. The message is not guaranteed to contain a null terminated string, and that's why you are seeing extraneous characters. The same sample you've pointed contains the right way of printing the byte array (which is assumed to contain printable characters):
(void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
Let me know if this answer works for you.
Best Regards,
Andrei Porumb
from azure-iot-sdk-c.
@anporumb You are right that works as intended on Linux.
But running the same code on a device that is not linux based, it seems the compiler does not like %.*s
and just returns:
Received Message [0] with Data: <<<*s>>> & Size=6
when sending a message: hello3
.
Instead, I'm converting the byte array to a string by appending '\0'
at the end:
static void bytearray_to_str(const unsigned char *buffer, int len)
{
char ret[len + 1];
strncpy(ret, buffer, len);
ret[len] = '\0';
printf("%s \n", ret);
}
WDYT?
from azure-iot-sdk-c.
Hello @ritazh ,
These are my observations: len
should be of type size_t
. Use memcpy
instead of strncpy
(for performance reasons, if nothing else). ret
is a C99 definition type (aka "flexible array", your compile rmight like it or not). Otherwise, looks good to me!
Best Regards,
Andrei Porumb
from azure-iot-sdk-c.
Thanks @anporumb! Here is my latest based on your feedback:
static unsigned char* bytearray_to_str(const unsigned char *buffer, size_t len)
{
unsigned char* ret = (unsigned char*)malloc(len+1);
memcpy(ret, buffer, len);
ret[len] = '\0';
//printf("%s \n", ret);
return ret;
}
Do you have any idea why %.*s
is not working for me?
from azure-iot-sdk-c.
Hello @ritazh,
The above code looks good to me.
I do not know why %.*s
does not work for you. %.*s in C means: "maximum number of bytes to be written for s conversion". Personally, I would contact the compiler's support.
Best Regards,
Andrei Porumb
from azure-iot-sdk-c.
Hello @ritazh ,
As this is not related to an SDK change, we will close this issue.
Best Regards,
Andrei Porumb
from azure-iot-sdk-c.
Hi, met the same issue and resolve it in the same way as suggested above.
@anporumb Though the issue close, I still want to ask, why the message is not null terminated guaranteed? Is there anything we could we do to make sure it nul terminated? Thank you.
from azure-iot-sdk-c.
Related Issues (20)
- SAS Token cannot be renewed MQTT
- Duplicate messages seen in Azure IoT Hub HOT 8
- missing branch: lts_08_2023 HOT 2
- "SDK API Reference Documentation" link is invalid HOT 4
- Getting ESP32 talking through Ethernet on SPI rather than WiFi for all comms HOT 2
- Azure -c- shared-utility on Raspberry pi HOT 5
- cleanup_prov_info function issue HOT 1
- Compile Error on Alpine HOT 2
- Sample for ESP32 HOT 1
- Incorrect EK Template; EK key does not match EKcert HOT 1
- Identity translation gateway HOT 5
- Unable to link VC runtime HOT 2
- IoTHubDeviceClient_LL_SetRetryPolicy and IoTHubDeviceClient_LL_GetRetryPolicy documentation HOT 2
- Mininum guaranteed date for DPS-TPM's "work as-is" ? HOT 2
- Undefined references from iothub_client_properties.h when included in C++ source file HOT 3
- iothub_client_sample_mqtt_dm sample generates unsafe header/library path used in cross-compilation error HOT 1
- OPTION_MESSAGE_TIMEOUT client option depecation HOT 3
- IoTHubDeviceClient_LL_UploadMultipleBlocksToBlob() segfaults. HOT 4
- Does this SDK client support empty proxy username and password? HOT 2
- Version 1.12.0 does not have a git tag HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-iot-sdk-c.