Thread: uint8_t omitting from 0x00 value

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    16

    uint8_t omitting from 0x00 value

    Hi guys,
    Im trying to print the variable packet. The packet variable contains hex value '0x00'. When i try to print the value unit8_t is trim the value from 0x00 to the end. In my case packet wont print because 'var_header' has first value '0x00'. If i take out the first '0x00' value , packet is printing upto next '0x00' value. If any of the value will get misses, the my mqtt broker wont connect. How to solve this?




    Below is the code:




    Code:
    #include <stdio.h>
    #include<stdint.h>
    #include <string.h>
    #define MQTTCONNECT 1<<4
    #define KEEPALIVE 15000
    void main()
    {
        char *id="arun";
        uint8_t var_header[] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03,0x02,0x00,KEEPALIVE/500,0x00,strlen(id)};
        uint8_t fixed_header[] = {MQTTCONNECT,12+strlen(id)+2};
        char packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id)];
        memset(packet,0,sizeof(packet));
        memcpy(packet,fixed_header,sizeof(fixed_header));
        memcpy(packet+sizeof(fixed_header),var_header,sizeof(var_header));
        memcpy(packet+sizeof(fixed_header)+sizeof(var_header),id,strlen(id));
        printf((uint8_t*)packet);
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Obviously you need to learn how printf works. Focus on %x format specifier.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    16
    Hi DRK,
    Thanks for the reply. I read the specifier for printf, "%u" is seems correct one, but didnt work.

    Code:
                printf("%u",(uint8_t*)packet);
    Code:
                output:
                2686684
    Last edited by mahece28; 06-23-2015 at 05:18 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Thanks for the reply. I read the specifier for printf, "%u" is seems correct one, but didnt work.
    Perhaps you need to increase your compiler warning level? Look at the warnings my compiler generates for your code.
    main.c||In function ‘main’:|
    main.c|11|warning: variable length array ‘packet’ is used [-Wvla]|
    main.c|16|warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint8_t *’ [-Wformat=]|
    ||=== Build finished: 0 errors, 2 warnings (0 minutes, 0 seconds) ===|
    It seems that "%u" is not the correct format specifier for your variable after all. Why are you trying to print a pointer with the "%u" format specifier? Perhaps that cast is part of your problem?

    Also note that main() should be defined to return an int and you should return an int from this function.

    Code:
    int main()
    {
    
       return 0;
    }
    Jim

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The issue is that you're trying to print your data as a string, and the embedded zeroes (falsely) indicate an end-of-string.

    Instead of printing them as a string, why not just print each element of the array in a loop? Or if you're transmitting this data, transmit the same way (send all elements in the array, one element at a time).

    "printf()" sounds like the wrong tool for the job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uint8_t to uint64_t
    By GeNcO in forum C Programming
    Replies: 5
    Last Post: 10-06-2011, 12:50 AM
  2. how to send a string with 0x00 in it?
    By ZuckMitnick in forum C Programming
    Replies: 9
    Last Post: 03-12-2011, 02:07 AM
  3. 0x00 in C
    By vandrea in forum C Programming
    Replies: 8
    Last Post: 08-16-2009, 05:20 AM
  4. Replies: 1
    Last Post: 06-09-2009, 12:55 AM
  5. omitting an empty line
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 04-19-2008, 12:33 PM