This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/EK-TM4C1294XL: Does char default unsigned 255 & iostream.h missing

Guru 56033 points
Part Number: EK-TM4C1294XL


Tool/software: TI C/C++ Compiler

So odd things occurring with hexadecimal bytes input from the outside world when MCU embedded C+ decodes char*.

So is char default to unsigned char via 18.12.3.LTS compiler?

Conversion and recombination of two hex bytes back into decimal integers fails profusely. For example serial input of 0xff (char*) ends up being 0x00 reading Buffer array cells 0xff was stored. Now that ain't a terrible thing since 0xff were only terminators 255,255,255 and are intact on first read but changed to 0x0 on second input pass.

However when NOT terminators (char*) 0xff must be left intact so the conversion of hexadecimal to decimal integers for variables and or reconstruction fails. Yet the outside world deconstruction of a single variable succeeds quit well when turned into two equivalent hexadecimal bytes for the MUC to serial import.

The main part of failure seems to be CCS and the MCU embedded language interpreter can not effectively reassemble two hex bytes given that <iostream> is not present in libc.a. How can anyone be expected to re-produce integer strings from hexadecimal bytes without #include <iostream.h> ? Does TI have some other scientific way to input hexadecimal to MCU for C+++ to manipulate back into variable integers. Otherwise very long strings of ASCII decimals must be passed to the MCU making the RX handler protocol highly invasive, bloated and prone to errors. In this case less is more in my book keep it KISS.

 

  • We have 28 hexadecimal bytes being imported to MCU via serial UART. That accounts for 8 variables each with '!' termination (char) or 16 data byes, unused 8 terminators at this time

    The hex data being read from array cells and converted to decimals integer are gibberish shifted or not. Yet playback of the import array is clear and exact to the 28 bytes transferred into MCU.

    uint16_t int0 = 0;
    
    /* Convert char to integer */
    int0 = HexToDec(CcRxBuff[6] );
    int0 = HexToDec(CcRxBuff[7] );
    //UARTprintf("Rx6:%x\n", CcRxBuff[6]);
    //UARTprintf("Rx7:%x\n", CcRxBuff[7]);
    int0 = (int0 >> 4) + (int1 << 4);
    g_sParameter = int0;
    
    // Function to convert hexadecimal to decimal
    int
    HexToDec(unsigned char hexVal)
    {
        // Initializing base value to 1, i.e 16^0
        char hex[0];
        *hex = hexVal;
        int base = 16;
        int i = 0;
        int dec_val = 0;
        int len = 0;
    
        len = strlen(hex);
    
        // Extracting characters as digits from last character   
        for(i=len-1; i >= 0; i--)
        {
            // if character lies in 0-99, converting
            // it to integral 0-9  value.
            if ((hex[i]>=0x00) && (hex[i]<=0x99)) 
            {
                dec_val += ((hex[i] ) * base); 
    
                // incrementing base by power
                base = base * 16;
            }
    
            // if character lies in 0a-ff, converting
            // it to integral 10 - 15  value.
            else if ((hex[i]>=0x0a) && (hex[i]<=0xff)) 
            {
                dec_val += ((hex[i]) * base); 
                // incrementing base by power
                base = base * 16;
            }
        }
        return (dec_val);
    }
    

  • BP101 said:
    So is char default to unsigned char via 18.12.3.LTS compiler?

    Yes.  I recommend you avoid plain char, and prefer signed char or unsigned char.

    The ARM compiler does supply the standard header file <iostream> .  There is no header file named <iostream.h>.

    Thanks and regards,

    -George

  • George Mock said:
    recommend you avoid plain char

    Yet every code piece developed by TI has used char or (char *) so it is 255 default not 127? Then all standard C++ conversions require (char*) when we specify unsigned char versus char. At lest the (char*) conversion doesn't cause MCU resets. 

    George Mock said:
    The ARM compiler does supply the standard header file <iostream>

    The c file type does not seem to recognize either one. I suspect only *.cpp can use <iostream> as a result. Needed to add stoi() that another engineer had mentioned was similar.  I suspect it too is not for conversion of RAW hexadecimal bytes to decimal.

    I was able to get the above code snip to work after major modifications since it was designed to convert ASCII hex, not simply hex to decimal. The base 16 byte positions of char[I] requires outside loop (<< high byte), (>> low byte) and adding them together. It seems to work ok for low values within a short char.