looking for itox() function, conversion of integer to hex string (no printf or stdio.h).

There is itoa() function from standard library that do this.

I'm using C28x device in embedded c (using code composer).

I'm looking for integer to hex string conversion

Can you suggest?

Example

UINT16 i;
INT32 RXdataRAW[10];    // contain INT32 data from ADC device
char UART_ASCII[12];       // for string formating.
for (i=0; i<10; i++)
{
UARTB_String("CH1 0x");
ltoa(RXdataRAW(i), UART_ASCII);
UARTB_Msg_LFCR(UART_ASCII);
}
Richard PayneChief Technology EngineerAsked:
Who is Participating?
 
ozoCommented:
static char d[]="0123456789abcdef";
void ltoa(unsigned long l,char a[9]){
  a[8]=0;
  for(int i=8;i>0;){
    a[--i]=d[l&0xf];
    l>>=4;
  }
}
int main(){
  char a[9];
  ltoa(0x12345678,a);
  printf("%s\n",a);
}
0
 
ozoCommented:
How are UARTB_String,  UARTB_Msg_LFCR, and ltoa defined?
0
 
Richard PayneChief Technology EngineerAuthor Commented:
itoa() come from standard library (stdlib.h), ie
int       ltoa(long val, char *buffer);

UARTB function is related to UARTB port of C28x based device. It take string in char array to UARTB port. That all you need to know....
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ozoCommented:
itoa is not standard.
There is a non-standard
char *  itoa ( int value, char * str, int base );
that is supported by some compilers,
but your example used ltoa
0
 
Richard PayneChief Technology EngineerAuthor Commented:
Hmm interesting

The data being used is 32 bit, the C28x device is 16 bit in memory but 32 bit core.

Thus the reason for ltoa() which is used for decimal ASCII, I need hex ASCII, they do not have base in ltoa()
0
 
sarabandeCommented:
or

char * intToHex(unsigned int x, char buf[], int sizbuf)
{      
    buf[--sizbuf] = '\0';
    while (sizbuf > 0 && x != 0)
    {
        unsigned char hexdig = (unsigned char)(x%16);
        buf[--sizbuf] =  (hexdig < 10)? ('0'+hexdig) : ('a' + hexdig-10);
        x /= 16;
    }
    return &buf[sizbuf];
}

int main()
{
    char buf[32] = { 0 };
    char * p = intToHex(0x1357ACEF, buf, 32);
    std::cout << p << std::endl;
    return 0;
}

Open in new window


Sara
0
 
Richard PayneChief Technology EngineerAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.