04
Dec
i64toa - Converts a 64 bit integer to string
Related Blog Items
- String to Integer (atoi)
- Convert integer to binary string
- Unsigned long to string (ultostr)
- Implicit Type Conversions - part 2
- Unsigned long long (uint64) to string (ulltostr)
Here is the program for converting a 64bit integer to string/char */ascii..
Please let me know any issues, suggestions..
//Converts a 64bit integer to a string
bool i64toa( int64 value, char * buf, int maxLen)
{
int lsbByte = 0;
char tempchr[10];
char tmpchar;
int count = 0;
int bufLen = 0;
bool negValue = false;
int cnt = -1;
if ((buf == NULL ) || ( maxLen == 0 ))
{
return false;
}
memset(buf, 0, maxLen );
memset(tempchar, 0x00, sizeof(tempchar));
if( value == 0 )
{
buf[0] = '0';
return true;
}
//If its a negative integer, convert it to positive and continue
if( value < 0 )
{
value = -value;
negValue = true;
buf[0] = '-';
buf++;
bufLen++;
}
//Extract byte by byte starting from the least significant byte
//of the 8 byte integerand form a string . The output string
//will contain the integer in the format least significant byte
//to most significant byte
while (value)
{
if ( bufLen++ > maxLen )
{
return false;
}
lsbByte = (int8)( value % 10 ) ;
value = value/10;
cnt = snprintf( tempchr, sizeof(tempchr),”%d”, abs(lsbByte) );
if ( ( cnt < 0 ) || ( cnt >= (int)sizeof(tempchr) ) )
{
return false;
}
strcat(buf, tempchr);
memset(tempchar, 0×00, sizeof(tempchar));
}
if (negValue)
{
bufLen–;
}
//Reverse the string to get the format most significant byte to
//least significant byte
for (count = 0; (count < (bufLen/2)); count++)
{
if (((bufLen - count - 1) < maxLen) &&
(count < maxLen))
{
tmpchar = buf[count];
buf[count] = buf [bufLen-count-1];
buf[bufLen-count-1] = tmpchar;
}
else
{
return false;
}
}
return true;
}
Download the source code i64toa.txt
Popularity: 12%
You need to log on to convert this article into PDF
Related Blog Items - String to Integer (atoi)
- Convert integer to binary string
- Unsigned long to string (ultostr)
- Implicit Type Conversions - part 2
- Unsigned long long (uint64) to string (ulltostr)
Related Blog Items
- String to Integer (atoi)
- Convert integer to binary string
- Unsigned long to string (ultostr)
- Implicit Type Conversions - part 2
- Unsigned long long (uint64) to string (ulltostr)
No Comments
No comments yet.
Sorry, the comment form is closed at this time.