有函数atoi可以实现10进制表示的字符串转化为10进制的数。
例如字符串str=“123”,那么atoi(str)结果为123。
现在我有这样两个字符串:
str1=“0xFF”(十六进制表示),str2=“0123”(八进制表示)
我想吧这两个字符串转化为10进制的整数,atoi好像不行,有没有其他的函数可以?
例如字符串str=“123”,那么atoi(str)结果为123。
现在我有这样两个字符串:
str1=“0xFF”(十六进制表示),str2=“0123”(八进制表示)
我想吧这两个字符串转化为10进制的整数,atoi好像不行,有没有其他的函数可以?
解决方案 »
- ACCESS数据库的问题
- $$$Clistctrl闪烁问题
- 如何实现点击左边的树型控件的节点,右边显示一个dialog界面
- mfc listbox与数据库
- 求助!在线等待,对话框中指针传递问题,我在对话框DLG中放了一个tabctrl控件,在TAB里添加了两项,即两个子对话框A,B,现在我想重A对话框调用DLG中的方法怎么做??
- win32项目中在一个类里如何获得主窗口的hwnd
- 高分请教循环缓冲
- 为什么在ListView中显示的Item看不见???
- 焦点问题
- CStatusDlg CStatusDlg(this)是什么意思?谢谢您!!!
- 那位好心人能当面教我做个图形软件呀。我已经一夜没有睡了
- 怎样绘制点线?
#include <stdlib.h>#include <stdio.h>int convert_string(char *str)
{
int index = 0;
int sum = 0; if(str == NULL)
return 0; while(str[index] != '\0')
{ //if the string has invalid characters like "abc123"
if(isalpha(str[index]))
{
//bringing upper case and lower case to lowercase
tolower(str[index]);
sum = sum + str[index] - 'a' + 1;
}
else
return 0; index++;
}
return sum;}int main()
{ printf("%d", convert_string("abc")); // output will be 6
printf("%d", convert_string("ab34"));//output will be 0}这样的代码修改一下就行了。
#include<stdlib.h>int convert_string(char str[])
{
int sum,i;if (str==NULL)
return 0;
else
{
for(i=0,sum=0;str[i]!='\0';i++)
sum+=1+str[i]-'a';
}
return sum;
}
void main()
{
char str[30];
printf("Enter string of alphabets only\n");
scanf("%s",&str);
printf("\nThe sum is %d",convert_string(str));
}
这个也行。