## Tips and Tricks (Part 21)

Previous T&T

Convert numerical values in strings of characters.

Converting numerical values into strings of characters is easy as far as Serial functions is concerned . However you  may face one day or another the need for converting a numerical value for insertion within an existing string of characters (e.g. LCD, commands sent through SPI, etc.). Here is a collection of converting functions and their Serial equivalents. But firstly, we need to create a buffer which will contain the output from the conversion functions:

```char *_vBuffer;

void setup(void)
{
_vBuffer = (char*) malloc(16 * sizeof(char));
Serial.begin(115200);
}```

These few lines of code constitute the header and the setup section of the code. And here are the converting instructions:

```/* Convert signed 32 bits integer */
ltoa(2863311530, _vBuffer, 10);
/* Results in -1431655766 */
Serial.println(_vBuffer);
/* Same as */
Serial.println(int32_t(2863311530));

/* Convert unsigned 32 bits integer */
ultoa(2863311530, _vBuffer, 10);
/* Results in 2863311530 */
Serial.println(_vBuffer);
/* Same as */
Serial.println(2863311530);

/* Convert 32 bits integer to hex */
itoa(2863311530, _vBuffer, 16);
/* Results in AAAAAAAA */
Serial.println(_vBuffer);
/* Same as */
Serial.println(2863311530, HEX);

/* Convert unsigned 16 bits integer */
utoa(43690, _vBuffer, 10);
/* Results in 43690 */
Serial.println(_vBuffer);
/* Same as */
Serial.println(43690);
/* Convert signed 16 bits integer */
itoa(43690, _vBuffer, 10);
/* Results in -21846 */
Serial.println(_vBuffer);
/* Same as */
Serial.println(int16_t(43690));
/* Convert integer to hex */
itoa(43690, _vBuffer, 16);
/* Results in AAAA */
Serial.println(_vBuffer);
/* Same as */
Serial.println(43690, HEX);

/* Convert float using 6 decimals */
dtostrf(M_PI, 8, 6, _vBuffer);
Serial.println(_vBuffer);
/* Same as */
Serial.println(M_PI, 6);
```

More details about the converting function here

HTH

Next Tip and Trick
{"ft_lang_label":"__label__en","ft_lang_prob":0.64930236,"math_prob":0.8868572,"size":1920,"snap":"2019-51-2020-05","text_gpt3_token_len":496,"char_repetition_ratio":0.21242172,"word_repetition_ratio":0.0701107,"special_character_ratio":0.35,"punctuation_ratio":0.20408164,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9717106,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-19T05:41:08Z\",\"WARC-Record-ID\":\"<urn:uuid:2ba2f0df-e63f-48fb-b0a1-e9c3a0c6a498>\",\"Content-Length\":\"33100\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6b122bbe-baf7-48c4-9e56-613853650c2b>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4fc603e-4d8c-4bef-8d9e-782ad8a007d9>\",\"WARC-IP-Address\":\"213.186.33.3\",\"WARC-Target-URI\":\"http://www.arduinoos.com/2014/12/tips-tricks-part-21/\",\"WARC-Payload-Digest\":\"sha1:EKJPCAIGSO2Z6ZABKDXZ5Q2CPVPEP3GY\",\"WARC-Block-Digest\":\"sha1:BNTNSXPW4BYQR3LNWDQHB5ZSZPPEXRES\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250594209.12_warc_CC-MAIN-20200119035851-20200119063851-00454.warc.gz\"}"}