隐蔽ASCII转换为int w/o atoi [英] Covert ASCII to Int w/o atoi

查看:152
本文介绍了隐蔽ASCII转换为int w/o atoi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我将输入的字符串值更改为ascii_to_int,我在输出中也得到689的恒定结果:

I am getting a constant result of 689 in the output even if i change the string values input into ascii_to_int:

#include<stdio.h>
#include<conio.h>

int ascii_to_int(unsigned char *s);

void main()
{
    clrscr();
    ascii_to_int("ATOI");
    printf("%d",ascii_to_int);
    getch();
}

int ascii_to_int(unsigned char *s)
{
     int value=0;
     while(*s>'0' && *s<'9')
     {
       value= value*10 + (*s-0);
       s++;
     }
     return value;
}



我做错了什么?

[修改:只需将<和>. &在& lt;已被更改为& amp; lt;]



What am I doing incorrectly?

[Modified: just fixed a < and >. The & in &lt; had been changed to &amp;lt;]

推荐答案

除了复制CPalini的答案外,还购买了两本书:

-Kernighan和Richie的"C编程语言"

-Peter van der Linden的专家C编程"

阅读它们并向内消化两者.第二篇是语法使用完后如何使用C的最佳方法之一,令人惊讶的是,对于一本技术书籍来说,这是一本非常有趣的文章.

干杯,

In addition to copying CPalini''s answer go and buy two books:

- "The C Programming Language" by Kernighan and Richie

- "Expert C Programming" by Peter van der Linden

Read them and inwardly digest both. The second one is one of the best treatments of how to use C once you''ve got the syntax down pat, and, surprisingly for a technical book it''s a really entertaining read.

Cheers,

Ash




我不确定在传递函数ASCII字符(即字母)然后检查0到9之间的值时您的代码打算做什么?但是无论如何,我确实注意到您已将要传递给"ascii_to_int"的参数声明为"unsigned char",但仍将其传递给"const char".
尝试单步操作以完全理解它,

希望对您有帮助
Hi,

I am not sure what your code is meant to do as you are passing your function ASCII characters (which are letters) then checking for values between 0 and 9? But anyway I did notice that you have declared the parameter which you are passing to ''ascii_to_int'' as an ''unsigned char'' and yet you are passing it a ''const char''.
Try to single step through it to understand it fully,

Hope that helps


您得到了错误的结果,因为您的代码中存在很多错误.
试试:

You got the wrong result because there are a lot of errors in you code.
Try:

#include <stdio.h>

int ascii_to_int(const char *s);

void main()
{
  printf("%d",ascii_to_int( "0101"));
  getchar();
}

int ascii_to_int(const char *s)
{
  int value=0;
  while(*s>='0' && *s<='9')
  {
    value = value * 10 + (*s-'0');
    s++;
  }
  return value;
}


这篇关于隐蔽ASCII转换为int w/o atoi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆