Integer to ASCII

From DSignT Support Database
Jump to: navigation, search

1 Integer - ASCII conversions without stdio

1.1 Integer to ASCII (decimal and hexadecimal output)

/***********************************************************************
 @brief	    convert an integer number to a decimal or hex string

 @param     dst - pointer to string
 @param	    val - value to convert
 @param     digits - number of decimal digits, negative value = hex format
 @return    pointer to string

 @note      max value: 32-bit
 @note 	    no terminating zero is added 
 @note      dst buffer must be large enough to hold the requested number
            of digits +2 for hex, +1 for decimal format
***********************************************************************/
char *itoa (char *dst, int64_t val, int8_t digits)
{
    /*******************************************************************
      locals
    *******************************************************************/
    char *p_str = dst;
    uint32_t u, tmp;
    int32_t sign, i;
    int8_t hex = 0;

    /*******************************************************************
      format:
        hex: add leading 0x
 	dec: add leading sign
    *******************************************************************/
    if (digits < 0)
    {
        hex = 1;
        digits = -digits;
        *p_str++ = '0';
        *p_str++ = 'x';
    }
    else
    {
        digits += 1;
    }
    
    /*******************************************************************
      sign, only if decimal
    *******************************************************************/
    if ((val < 0) && (hex == 0))
    {
 	u = ((uint32_t)-(val+1)) + 1; 
 	sign = -1;
    }
    else
    {
	u = (uint32_t)val;
	sign = +1;
    }


    /*******************************************************************
      output
    *******************************************************************/
    p_str += digits;
    
    if (hex == 0)
    {
        /***************************************************************
          write as decimal without leading zeroes
        ***************************************************************/
        do
        {
            tmp = u % 10;
            *--p_str = (char)tmp+'0';
            u /= 10; 
        } while (u > 0);

        if (sign < 0)
        {
            *--p_str = '-';
        }

        while (p_str > dst)
        {
            *--p_str = ' ';
        }
    }
    else
    {
        /***************************************************************
          write as hex 
        ***************************************************************/
        for (i=0; i<digits; i++)
        {
            tmp = u & 0x0F;
            if (tmp < 10)
                *--p_str = (char)tmp + '0';
            else
                *--p_str = (char)tmp + 'A' - 10;
            u = u >> 4;
        }
    }
    
    return dst;
}


1.2 Hex to integer

/***********************************************************************
 @brief     convert hex string to 32-bit unsigned interger

 @param     str - pointer to string with hex-value
 @return    unsigned integer 32-bit

 @note      no range checking !
***********************************************************************/
uint32_t hextou32 (char *str)
{
    /*******************************************************************
      Locals
    *******************************************************************/
    uint32_t result = 0;
    uint32_t temp;
    char *p_str = str;
    char c;
	
    /*******************************************************************
      convert digits until terminating 0
      ignore leading whitespaces
      ignore leading 0x 
    *******************************************************************/
    while (c = *p_str++)
    {
 	if ((c != ' ') && (c != '\9'))
	{
    	    // convert to uppercase
	    if (c >= 'a') c -= ('a'-'A');
			
	    if (c == 'X')
	    {
		result = 0;
	    }
	    else
	    {
		// convert to decimal
		temp = (c < 'A')? c-'0': c-'A'+10;
		
		// merge result
		result = result << 4;
		result |= temp;
	    }
	}
    }
    return (result);
}

1.3 Hex/ASCII Dump via UART

/***********************************************************************
 @brief	    helper function: convert an 8-bit byte to base-16 ASCII 

 @param	    x - value to convert
 @return    pointer to string
***********************************************************************/
char *u8tohex (uint8_t x)
{
    /*******************************************************************
      locals
    *******************************************************************/
    static char s[3];
    uint8_t temp;

    /*******************************************************************
      convert
    *******************************************************************/
    temp = x >> 4;
    if (temp > 9)
        temp += 'A'-10;
    else
        temp += '0';
    s[0] = temp;

    temp = x & 0x0F;
    if (temp > 9)
        temp += 'A'-10;
    else
        temp += '0';
    s[1] = temp;

    s[2] = 0;
    return (s);
}



/***********************************************************************
 @brief	    ASCII + Hex dump via UART

 @param	    buffer - pointer to buffer
 @param     count  - number of bytes to display
 @return    -

 @note      calls DM2_uartWriteStr
***********************************************************************/
void dump (char* buffer, size_t count)
{
    int32_t i, j;
    char c;

    DM2_uartWriteStr(h_uart, "\r\nBuffer:", DM2_UART_NOTIMEOUT);
    if (count == 0)
    {
        DM2_uartWriteStr(h_uart, "empty\r\n", DM2_UART_NOTIMEOUT);
    }
    else
    {
        DM2_uartWriteStr(h_uart, "\r\n      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F - ASCII\r\n", DM2_UART_NOTIMEOUT);
        for (i=0; i<(count/16); i++)
        {
            DM2_uartWriteStr(h_uart, u8tohex((uint8_t)i), DM2_UART_NOTIMEOUT);
            DM2_uartWriteStr(h_uart, "0 -", DM2_UART_NOTIMEOUT);
            for (j=0; j<16; j++)
            {
                DM2_uartWriteStr(h_uart, " ", DM2_UART_NOTIMEOUT);
                DM2_uartWriteStr(h_uart, u8tohex(buffer[16*i + j]), DM2_UART_NOTIMEOUT);
            }
            DM2_uartWriteStr(h_uart, " - ", DM2_UART_NOTIMEOUT);
            for (j=0; j<16; j++)
            {
                c = buffer[16*i + j];
                if (!isprint(c)) c = '.';
                while (!DM2_uartWrite(h_uart, c)) ;
            }
            DM2_uartWriteStr(h_uart, "\r\n", DM2_UART_NOTIMEOUT);
        }
    }
    DM2_uartWriteStr(h_uart, "\r\n", DM2_UART_NOTIMEOUT);
}


Contact Post.png Support Tool.png