#include <iostream.h>static void xtoa (
long val,
char *buf,
unsigned radix
)
{
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */ p = buf; if (val<0) {
/* negative, so output '-' and negate */
*p++ = '-';
val = -(long)val;
} firstdig = p; /* save pointer to first digit */ do {
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */ /* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
} while (val > 0); /* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */ *p-- = '\0'; /* terminate string; p points to last digit */ do {
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
} while (firstdig < p); /* repeat until halfway */
}void main()
{
long n;
char *buf;
cin>>n;
xtoa(n,buf,16);
cout<<buf<<"\n";
}
long val,
char *buf,
unsigned radix
)
{
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */ p = buf; if (val<0) {
/* negative, so output '-' and negate */
*p++ = '-';
val = -(long)val;
} firstdig = p; /* save pointer to first digit */ do {
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */ /* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
} while (val > 0); /* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */ *p-- = '\0'; /* terminate string; p points to last digit */ do {
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
} while (firstdig < p); /* repeat until halfway */
}void main()
{
long n;
char *buf;
cin>>n;
xtoa(n,buf,16);
cout<<buf<<"\n";
}
解决方案 »
- 文件读写问题
- 请教如何把一个字符转换成相同的十六进制数
- 我新建工程时选择了windows sockets设置。由于某些原因我必须把它删掉,请问有谁知道如何去掉啊?
- 散分 老师布置的题目 不能懂 谁能帮我理下 谢谢 谢谢
- 树视控件
- 如何新建一个文件夹?
- 在win32 Dynamic-Link Library中,CoGetClassObject()是不是会自动调用DllGetClassObject()??
- 如何杀掉由某个Service程序运行起来的进程!急!急!
- cedite 类 增大字体,只能显示一半
- 请教各位类型安全是什么意思?
- OGL缓存交换的两个函数
- 如何用OPENGL读入带有ALPHA通道的纹理?
都是空指针在搞来搞去