Giter Site home page Giter Site logo

Comments (9)

anporumb avatar anporumb commented on May 16, 2024

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.

ritazh avatar ritazh commented on May 16, 2024

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

anporumb avatar anporumb commented on May 16, 2024

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.

ritazh avatar ritazh commented on May 16, 2024

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

anporumb avatar anporumb commented on May 16, 2024

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.

ritazh avatar ritazh commented on May 16, 2024

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.

anporumb avatar anporumb commented on May 16, 2024

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.

anporumb avatar anporumb commented on May 16, 2024

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.

SummerSun avatar SummerSun commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.