//char *_itoa( int value, char *string, int radix );
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s\n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s\n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s\n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s\n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
5.1myitoa
复制代码
char * myitoa( int value, char *string, int radix )
{
char* str = string;
int sign = value, i = 0, j = 0;
if(sign < 0)
{
value = -value;
sign = -1;
}
else
{
sign = 1;
}
while(value)
{
if(value < radix)
{
*str++ = value + '0';
break;
}
*str++ = value % radix + '0';
value = value / radix;
}
if(sign == -1)
{
*str++ = '-';
}
*str = '\0';
j = str - string -1;
for(i = 0; i < j; i++)
{
int tmp = str[i];
str[i] = str[j];
str[j--] = tmp;
}
return str;
}
6.memmove
复制代码
void *memmove( void *dest, const void *src, size_t count );
#include <memory.h>
#include <string.h>
#include <stdio.h>
char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/* 1 2 3 4 5
* 12345678901234567890123456789012345678901234567890
*/
void main( void )
{
printf( "Function:\tmemcpy without overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );
printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}
Output
Function: memcpy without overlap
Source: fox
Destination: dog jumps over the lazy fox
Result: The quick brown fox jumps over the lazy fox
Length: 43 characters
Function: memmove with overlap
Source: quick brown fox jumps over the lazy dog
Destination: brown fox jumps over the lazy dog
Result: The quick quick brown fox jumps over the lazy dog
Length: 49 characters
Function: memcpy with overlap
Source: quick brown dog jumps over the lazy fox
Destination: brown dog jumps over the lazy fox
Result: The quick quick brown dog jumps over the lazy fox
Length: 49 characters
6.1mymemmove
复制代码
void* mymemmove(void* dest, const void* src, size_t n) {
// 类型转换,便于操作
unsigned char* d = (unsigned char*)dest;
const unsigned char* s = (const unsigned char*)src;
// 如果目标和源相同,或者移动的字节数为 0,直接返回
if (d == s || n == 0) {
return dest;
}
// 处理内存重叠的情况
if (d < s) {
// 如果目标地址在源地址之前,正常从前到后复制
for (size_t i = 0; i < n; i++) {
d[i] = s[i];
}
} else {
// 如果目标地址在源地址之后,反向复制
for (size_t i = n; i > 0; i--) {
d[i - 1] = s[i - 1];
}
}
return dest;
}