@OP

You may find the following examples helpful to understand the working mechanism of the itoa() function:

itoa() Function

In the context of itoa()b function, integer data refers to a data in the human world, which has no fractional part and the digits of the integer part are confined within 0 to 9. The number could be a positive or negative. Thus, 1234 is an integer data; 0x6B7C is also an integer data as it is actually 27516 in the human world; 0xB37C is also an integer as it is actually -19587 in the human world. The possible range of 4-digit integer data is: 0x8000 - 0x7FFF (-32768 to 32767).

The function prototype of itoa() (Integer To Ascii) function as given in the stdlib.h Library is:

`char *_CType itoa(int __value, char *__string, int __radix);`

What does the function do? Assume that the argument-3 (radix = base) is 10; the itoa() function transforms the value of argument-1 into decimal digits (the decimal number); converts the digits of the decimal number into their respective ASCII codes; saves the ASCII codes in a character type array pointed by argument-2; places a null-character/null-byte ('\0'/0x00) as the last element of the array. The function will also return a numerical value (known as pointer) which is the base address of the array being pointed by argument-2.

Example-1: Assume the argument-1 is 0x6B7C; it will be transformed into a decimal number of 27516; the digits will be converted to their respective ASCII codes like these: for 2, there will be 0x32; for 7, there will be 0x37; for 5, there will be 0x35; for 1, there will be 0x31; for 6, there will be 0x36. The Arduino codes are:

`char *str; //str is a pointer variable; it will hold numerical value returned by function`

char myData[20]; //this array will hold the ASCII codes generated by the function

char *ptr; //ptr is pointer variable; it points myData[] array and holds its base address

int x = &myData;

ptr = x; //now the pointer variable ptr holds the base address of myData[] array

Serial.println(x, HEX); // shows base address in numerical form of myData[] array

str = itoa(0x6B7C, ptr, 10); //or str = itoa(0x6B7C, myData, 10); or itoa(0x6B7C, myData, 10);

int y = str;

Serial.println (y, HEX); //shows base address of myData[] array; it should be equal to x

Serial.println(myData); //shows: 27516

Serial.println(myData[0], HEX); //shows: 0x32 - the ASCII code of 2

Example-2: Assume the argument-1 is 0xB37C; this number (and any other number with LH at MSBit) will be considered as the 2's complement form of a negative decimal number; it will be transformed into a decimal number of -19587; the minus_sign and digits will be converted to their respective ASCII codes like these: for minus_sign (-), there will be 0x2D; for 1, there will be 0x31; for 9, there will be 0x39; for 5, there will be 0x35; for 8, there will be 0x38; for 7, there will be 0x37. The Arduino codes are similar to Example-1.

Example-3: Convert the number 0x6B7C into an ASCII string without using itoa() function. Save the result in the character type array myData[ ].

`Char myArray[20]=""; //initialized with 0x00 in all locations`

int z = 0x6B7C;

for (i = 4; i>=0; i--)

{

myArray[i] = (z % 10) + 0x30; //modulus (%) and division (/) operations

z = z / 10;

}

Serial.println(myArray); //shows: 27516