Convert uint8_t to an ascii string C Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Is there a better way to size a buffer for printing integers?trying to copy a char pointer using memcpy, getting an errorC#, extract double from USARTs receive char bufferCppUTest: how to pass more data to a specific mock call?Confusions about if statement and strcmp with StringsSend converted DS1820 temperature over PIC16 uartC++ unsigned char array lengthC:Having some troubles with a pointer to uint8_t[]Sending a int8_t as a uint8_t without changing bit patternfacing a challenge writing my own and my_itoa and my atoi, which should change integer to ascii and vice versa in CVariable size of uint8_t array

Why is it faster to reheat something than it is to cook it?

Illegal assignment from sObject to Id

Disembodied hand growing fangs

Selecting user stories during sprint planning

How does Python know the values already stored in its memory?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Find 108 by using 3,4,6

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Using audio cues to encourage good posture

How to compare two different files line by line in unix?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Why is the AVR GCC compiler using a full `CALL` even though I have set the `-mshort-calls` flag?

What was the first language to use conditional keywords?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

Hangman Game with C++

How does light 'choose' between wave and particle behaviour?

ArcGIS Pro Python arcpy.CreatePersonalGDB_management

Dating a Former Employee

What is this clumpy 20-30cm high yellow-flowered plant?

Putting class ranking in CV, but against dept guidelines

What order were files/directories outputted in dir?

Why weren't discrete x86 CPUs ever used in game hardware?

Is there any word for a place full of confusion?



Convert uint8_t to an ascii string C



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Is there a better way to size a buffer for printing integers?trying to copy a char pointer using memcpy, getting an errorC#, extract double from USARTs receive char bufferCppUTest: how to pass more data to a specific mock call?Confusions about if statement and strcmp with StringsSend converted DS1820 temperature over PIC16 uartC++ unsigned char array lengthC:Having some troubles with a pointer to uint8_t[]Sending a int8_t as a uint8_t without changing bit patternfacing a challenge writing my own and my_itoa and my atoi, which should change integer to ascii and vice versa in CVariable size of uint8_t array



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Given the following function



UART_write(UART_Handle handle, const void *buffer, size_t size);


I want to send via uart a int8_t value ( log it )



What i tried:



int8_t value;
UART_write(uart, value, strlen(value));



const char *echoPrompt = (char *)value;
UART_write(uart, echoPrompt, sizeof(echoPrompt));



const char echoPrompt2[] = value;
UART_write(uart, echoPrompt2, sizeof(echoPrompt2));



const char* buff = value;
UART_write(uart, value, strlen(value));


The best i got is logging the hex value



Exemple of how the uart_write function works: In orded to log "12" what I need to do is



 const uint8_t value[] = 0x31, 0x32;
UART_write(uart, value, sizeof(value));


So my question is, how to log my int8_t variable ( I need to log negative numbers as well)










share|improve this question

















  • 1





    Did you look here?

    – alk
    Mar 22 at 11:05

















0















Given the following function



UART_write(UART_Handle handle, const void *buffer, size_t size);


I want to send via uart a int8_t value ( log it )



What i tried:



int8_t value;
UART_write(uart, value, strlen(value));



const char *echoPrompt = (char *)value;
UART_write(uart, echoPrompt, sizeof(echoPrompt));



const char echoPrompt2[] = value;
UART_write(uart, echoPrompt2, sizeof(echoPrompt2));



const char* buff = value;
UART_write(uart, value, strlen(value));


The best i got is logging the hex value



Exemple of how the uart_write function works: In orded to log "12" what I need to do is



 const uint8_t value[] = 0x31, 0x32;
UART_write(uart, value, sizeof(value));


So my question is, how to log my int8_t variable ( I need to log negative numbers as well)










share|improve this question

















  • 1





    Did you look here?

    – alk
    Mar 22 at 11:05













0












0








0








Given the following function



UART_write(UART_Handle handle, const void *buffer, size_t size);


I want to send via uart a int8_t value ( log it )



What i tried:



int8_t value;
UART_write(uart, value, strlen(value));



const char *echoPrompt = (char *)value;
UART_write(uart, echoPrompt, sizeof(echoPrompt));



const char echoPrompt2[] = value;
UART_write(uart, echoPrompt2, sizeof(echoPrompt2));



const char* buff = value;
UART_write(uart, value, strlen(value));


The best i got is logging the hex value



Exemple of how the uart_write function works: In orded to log "12" what I need to do is



 const uint8_t value[] = 0x31, 0x32;
UART_write(uart, value, sizeof(value));


So my question is, how to log my int8_t variable ( I need to log negative numbers as well)










share|improve this question














Given the following function



UART_write(UART_Handle handle, const void *buffer, size_t size);


I want to send via uart a int8_t value ( log it )



What i tried:



int8_t value;
UART_write(uart, value, strlen(value));



const char *echoPrompt = (char *)value;
UART_write(uart, echoPrompt, sizeof(echoPrompt));



const char echoPrompt2[] = value;
UART_write(uart, echoPrompt2, sizeof(echoPrompt2));



const char* buff = value;
UART_write(uart, value, strlen(value));


The best i got is logging the hex value



Exemple of how the uart_write function works: In orded to log "12" what I need to do is



 const uint8_t value[] = 0x31, 0x32;
UART_write(uart, value, sizeof(value));


So my question is, how to log my int8_t variable ( I need to log negative numbers as well)







c serial-port embedded uart






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 10:32









andrei filipandrei filip

62




62







  • 1





    Did you look here?

    – alk
    Mar 22 at 11:05












  • 1





    Did you look here?

    – alk
    Mar 22 at 11:05







1




1





Did you look here?

– alk
Mar 22 at 11:05





Did you look here?

– alk
Mar 22 at 11:05












2 Answers
2






active

oldest

votes


















3














You will need to convert your integer to string.



snprintf is a standard way to do this, if your libc provides it.






share|improve this answer






























    0















    Convert uint8_t to an ascii string C




    Determine the maximum string size needed for any value of that type. Is there a better way to size a buffer for printing integers?



    #define UINT_BUFFER10_SIZE(type) (1 + (CHAR_BIT*sizeof(type)*LOG10_2_N)/LOG10_2_D + 1)


    Form the buffer



    char buf[UINT_BUFFER10_SIZE(value)];


    "Print" the uint8_t to the buffer.



    int len = sprintf(buf, "%d", value);
    // or pedantically
    int len = snprintf(buf, sizeof buf, "%" PRId8, value); // see <inttypes.h>
    assert(len >= 0 && (unsigned)len < sizeof buf);


    Send it



    UART_write(uart, buf, len);




    how to log my int8_t variable




    #define INT_BUFFER10_SIZE(type) (2 + ((CHAR_BIT*sizeof(type)-1)*LOG10_2_N)/LOG10_2_D + 1)
    char buf[INT_BUFFER10_SIZE(ivalue)];
    int len = sprintf(buf, "%d", ivalue);
    UART_write(uart, buf, len);



    IMO, code should add a helper function to send a string



    void UART_write_str(UART_Handle handle, const char *str) 
    UART_write(uart, str, strlen(str));






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297698%2fconvert-uint8-t-to-an-ascii-string-c%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You will need to convert your integer to string.



      snprintf is a standard way to do this, if your libc provides it.






      share|improve this answer



























        3














        You will need to convert your integer to string.



        snprintf is a standard way to do this, if your libc provides it.






        share|improve this answer

























          3












          3








          3







          You will need to convert your integer to string.



          snprintf is a standard way to do this, if your libc provides it.






          share|improve this answer













          You will need to convert your integer to string.



          snprintf is a standard way to do this, if your libc provides it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 10:47









          domendomen

          1,215816




          1,215816























              0















              Convert uint8_t to an ascii string C




              Determine the maximum string size needed for any value of that type. Is there a better way to size a buffer for printing integers?



              #define UINT_BUFFER10_SIZE(type) (1 + (CHAR_BIT*sizeof(type)*LOG10_2_N)/LOG10_2_D + 1)


              Form the buffer



              char buf[UINT_BUFFER10_SIZE(value)];


              "Print" the uint8_t to the buffer.



              int len = sprintf(buf, "%d", value);
              // or pedantically
              int len = snprintf(buf, sizeof buf, "%" PRId8, value); // see <inttypes.h>
              assert(len >= 0 && (unsigned)len < sizeof buf);


              Send it



              UART_write(uart, buf, len);




              how to log my int8_t variable




              #define INT_BUFFER10_SIZE(type) (2 + ((CHAR_BIT*sizeof(type)-1)*LOG10_2_N)/LOG10_2_D + 1)
              char buf[INT_BUFFER10_SIZE(ivalue)];
              int len = sprintf(buf, "%d", ivalue);
              UART_write(uart, buf, len);



              IMO, code should add a helper function to send a string



              void UART_write_str(UART_Handle handle, const char *str) 
              UART_write(uart, str, strlen(str));






              share|improve this answer





























                0















                Convert uint8_t to an ascii string C




                Determine the maximum string size needed for any value of that type. Is there a better way to size a buffer for printing integers?



                #define UINT_BUFFER10_SIZE(type) (1 + (CHAR_BIT*sizeof(type)*LOG10_2_N)/LOG10_2_D + 1)


                Form the buffer



                char buf[UINT_BUFFER10_SIZE(value)];


                "Print" the uint8_t to the buffer.



                int len = sprintf(buf, "%d", value);
                // or pedantically
                int len = snprintf(buf, sizeof buf, "%" PRId8, value); // see <inttypes.h>
                assert(len >= 0 && (unsigned)len < sizeof buf);


                Send it



                UART_write(uart, buf, len);




                how to log my int8_t variable




                #define INT_BUFFER10_SIZE(type) (2 + ((CHAR_BIT*sizeof(type)-1)*LOG10_2_N)/LOG10_2_D + 1)
                char buf[INT_BUFFER10_SIZE(ivalue)];
                int len = sprintf(buf, "%d", ivalue);
                UART_write(uart, buf, len);



                IMO, code should add a helper function to send a string



                void UART_write_str(UART_Handle handle, const char *str) 
                UART_write(uart, str, strlen(str));






                share|improve this answer



























                  0












                  0








                  0








                  Convert uint8_t to an ascii string C




                  Determine the maximum string size needed for any value of that type. Is there a better way to size a buffer for printing integers?



                  #define UINT_BUFFER10_SIZE(type) (1 + (CHAR_BIT*sizeof(type)*LOG10_2_N)/LOG10_2_D + 1)


                  Form the buffer



                  char buf[UINT_BUFFER10_SIZE(value)];


                  "Print" the uint8_t to the buffer.



                  int len = sprintf(buf, "%d", value);
                  // or pedantically
                  int len = snprintf(buf, sizeof buf, "%" PRId8, value); // see <inttypes.h>
                  assert(len >= 0 && (unsigned)len < sizeof buf);


                  Send it



                  UART_write(uart, buf, len);




                  how to log my int8_t variable




                  #define INT_BUFFER10_SIZE(type) (2 + ((CHAR_BIT*sizeof(type)-1)*LOG10_2_N)/LOG10_2_D + 1)
                  char buf[INT_BUFFER10_SIZE(ivalue)];
                  int len = sprintf(buf, "%d", ivalue);
                  UART_write(uart, buf, len);



                  IMO, code should add a helper function to send a string



                  void UART_write_str(UART_Handle handle, const char *str) 
                  UART_write(uart, str, strlen(str));






                  share|improve this answer
















                  Convert uint8_t to an ascii string C




                  Determine the maximum string size needed for any value of that type. Is there a better way to size a buffer for printing integers?



                  #define UINT_BUFFER10_SIZE(type) (1 + (CHAR_BIT*sizeof(type)*LOG10_2_N)/LOG10_2_D + 1)


                  Form the buffer



                  char buf[UINT_BUFFER10_SIZE(value)];


                  "Print" the uint8_t to the buffer.



                  int len = sprintf(buf, "%d", value);
                  // or pedantically
                  int len = snprintf(buf, sizeof buf, "%" PRId8, value); // see <inttypes.h>
                  assert(len >= 0 && (unsigned)len < sizeof buf);


                  Send it



                  UART_write(uart, buf, len);




                  how to log my int8_t variable




                  #define INT_BUFFER10_SIZE(type) (2 + ((CHAR_BIT*sizeof(type)-1)*LOG10_2_N)/LOG10_2_D + 1)
                  char buf[INT_BUFFER10_SIZE(ivalue)];
                  int len = sprintf(buf, "%d", ivalue);
                  UART_write(uart, buf, len);



                  IMO, code should add a helper function to send a string



                  void UART_write_str(UART_Handle handle, const char *str) 
                  UART_write(uart, str, strlen(str));







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 22 at 14:50

























                  answered Mar 22 at 14:44









                  chuxchux

                  85.3k874157




                  85.3k874157



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297698%2fconvert-uint8-t-to-an-ascii-string-c%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript