64,641
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
char main ()
{
int a ;
cin>>a;
char b[10];
int i=0;
for(a;a!=0;a=a/16)
{
if(a%16>=10)
{
switch(a%16)
{
case 10:b[i]='A';break;
case 11:b[i]='B';break;
case 12:b[i]='C';break;
case 13:b[i]='D';break;
case 14:b[i]='E';break;
case 15:b[i]='F';break;
default : return 0;
}
}
else
b[i]=a%16+'0'; i++;
}
for (int j=i-1;j>=0;j--) cout<<b[j];
return 0;
}
char * ltoa ( long value, char * buffer, int radix );
Convert long integer value to string.
Converts a long integer value to a null-terminated string using the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by the minus sign (-). With any other radix, value is always considered unsigned.
buffer should be large enough to contain any possible value.
Parameters.
value
Value to be represented as a string.
buffer
Buffer where to store the resulting string.
radix
Numeral radix in which value has to be represented, between 2 and 36.
Return Value.
A pointer to the string.
Example.
//Example for function ltoa
#include <iostream.h>
#include <stdlib.h>
int main()
{
long num=16021977;
char buffer[40];
ltoa(num, buffer, 10);
cout<<"decimal: "<<buffer<<endl;
ltoa(num, buffer, 16);
cout<<"hexadecimal: "<<buffer<<endl;
ltoa(num, buffer, 2);
cout<<"binary: "<<buffer<<endl;
system("PAUSE");
return 0;
}
Output:
decimal: 16021977
hexadecimal: f479d9
binary: 111101000111100111011001