7 个解决方案
#1
sscanf
#2
root@~ #cat 2.c
root@~ #./2
3200
root@~ #
#include <stdio.h>
int main (void) {
char table[]="0123456789abcdef";
char str[]="0c80";
int i,j,len=0,m=1,result=0;
while(str[len]!='\0') {
len++;
}
i=len;
for(i--;i>=0;i--) {
for(j=0;table[j]!='\0';j++) {
if(str[i]==table[j]) {
result+=j*m;
}
}
m*=16;
}
printf ("%i\n",result);
return 0;
}
root@~ #./2
3200
root@~ #
#3
用atoi函数,选择你要转换的进制...
#4
网上有例题
#5
要装换16进制的可以用strtol()函数,atoi只能转换十进制的!
#6
#include <stdlib.h>
long int
strtol(const char *nptr, char **endptr, int base);
long long int
strtoll(const char *nptr, char **endptr, int base);
DESCRIPTION
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2
and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional `+' or `-' sign. If base
is zero or 16, the string may then include a `0x' prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal)
unless the next character is `0', in which case it is taken as 8 (octal).
The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in
the given base. (In bases above 10, the letter `A' in either upper or lower case represents 10, `B' represents 11, and so forth, with `Z' repre-
senting 35.)
long int
strtol(const char *nptr, char **endptr, int base);
long long int
strtoll(const char *nptr, char **endptr, int base);
DESCRIPTION
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2
and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional `+' or `-' sign. If base
is zero or 16, the string may then include a `0x' prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal)
unless the next character is `0', in which case it is taken as 8 (octal).
The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in
the given base. (In bases above 10, the letter `A' in either upper or lower case represents 10, `B' represents 11, and so forth, with `Z' repre-
senting 35.)
#7
#include <iostream>
using namespace std;
int simpleConvert(const char *str)
{
int sum=0;
for(int i=0;str[i]!=0;++i)
{
sum<<=4;
if(0<=str[i] && str[i]<='9')
{
sum|=str[i]-'0';
}
else
{
sum|=str[i]-'A'+10;
}
}
return sum;
}
int main(int argc, char* argv[])
{
char buffer[100]="0C80";
cout<<simpleConvert(buffer)<<endl;
return 0;
}
16进制是严格与二进制,十进制挂钩的,就这么简单。
#1
sscanf
#2
root@~ #cat 2.c
root@~ #./2
3200
root@~ #
#include <stdio.h>
int main (void) {
char table[]="0123456789abcdef";
char str[]="0c80";
int i,j,len=0,m=1,result=0;
while(str[len]!='\0') {
len++;
}
i=len;
for(i--;i>=0;i--) {
for(j=0;table[j]!='\0';j++) {
if(str[i]==table[j]) {
result+=j*m;
}
}
m*=16;
}
printf ("%i\n",result);
return 0;
}
root@~ #./2
3200
root@~ #
#3
用atoi函数,选择你要转换的进制...
#4
网上有例题
#5
要装换16进制的可以用strtol()函数,atoi只能转换十进制的!
#6
#include <stdlib.h>
long int
strtol(const char *nptr, char **endptr, int base);
long long int
strtoll(const char *nptr, char **endptr, int base);
DESCRIPTION
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2
and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional `+' or `-' sign. If base
is zero or 16, the string may then include a `0x' prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal)
unless the next character is `0', in which case it is taken as 8 (octal).
The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in
the given base. (In bases above 10, the letter `A' in either upper or lower case represents 10, `B' represents 11, and so forth, with `Z' repre-
senting 35.)
long int
strtol(const char *nptr, char **endptr, int base);
long long int
strtoll(const char *nptr, char **endptr, int base);
DESCRIPTION
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2
and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional `+' or `-' sign. If base
is zero or 16, the string may then include a `0x' prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal)
unless the next character is `0', in which case it is taken as 8 (octal).
The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in
the given base. (In bases above 10, the letter `A' in either upper or lower case represents 10, `B' represents 11, and so forth, with `Z' repre-
senting 35.)
#7
#include <iostream>
using namespace std;
int simpleConvert(const char *str)
{
int sum=0;
for(int i=0;str[i]!=0;++i)
{
sum<<=4;
if(0<=str[i] && str[i]<='9')
{
sum|=str[i]-'0';
}
else
{
sum|=str[i]-'A'+10;
}
}
return sum;
}
int main(int argc, char* argv[])
{
char buffer[100]="0C80";
cout<<simpleConvert(buffer)<<endl;
return 0;
}
16进制是严格与二进制,十进制挂钩的,就这么简单。