我如何制作一个将文本转换为ascii数字的项目 [英] how i make a project that convert text to ascii numbers

查看:85
本文介绍了我如何制作一个将文本转换为ascii数字的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ascii数字到文本

文本到ascii数字

输入:测试用例

输出:116 101 115 116 032 099 097 115 101

解决方案

对于我的实验,我接受了输入作为参数,并使用 for 循环迭代它作为Griff显示,但以下是显示每个字符的ASCII值所需的全部内容:



 printf(%03d ,argv [1] [i])


我们不做你的作业:它是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是你想的很难!



但是......一些提示:

这不是一项艰巨的任务:你几乎肯定知道如何从用户读取一个字符串,所以没有必要提到那个位。



当你读入字符串时,它位于一个字符数组中,所以你需要一个循环依次查看每个字符:for循环可能是最好的:

  int  index; 
...
for (index = 0 ; lineFromUser [index]! = ' \0'; index ++)
{
...
}

在循环中,您可以通过查看lineFromUser [index]依次获取每个角色 - 这将依次为您提供每个角色。

您可能知道如何使用printf或类似方法输出数字?所以我也不需要掩盖这一点。

你需要做的就是将字符转换为整数并输出它,然后输出空格......

 myInteger =( int )myCharacter; 

应该这样做......


嗯,这可能是这样的:

  int  t_as_int =(< span class =code-keyword> int )'  t'; 
int e_as_int =( int ' e';
int s_as_int =( int ' s';
int t_as_int =( int ' t';
.........



因此,这是将字符转换为等效的ascii数字的基本过程。因为你需要这种情况下的文本,所以循环通过你的文本并获取字符,然后通过上面的方式将其转换为ascii。就是这样,希望你能理解现在该做什么。


ascii numbers to text
text to ascii numbers
input: test case
output: 116 101 115 116 032 099 097 115 101

解决方案

For my experiment, I accepted the input as a parameter and iterated it with a for loop as Griff Showed, but the following is all you need to display the "ASCII value" of each character:

printf(" %03d",argv[1][i])


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But...some hints:
This isn't a difficult task: you almost certainly know how to read a string in from the user, so there is no need to mention that bit.

When you read the string in, it sits in an array of characters, so you are going to need a loop to look at each character in turn: a for loop is probably the best:

int index;
...
for (index = 0; lineFromUser[index] != '\0'; index++)
   {
   ...
   }

Inside the loop, you can get each character in turn by looking at lineFromUser[index] - which will give you each character in turn.
You presumably know how to output a number using printf or similar? So I don't need to cover that bit, either.
All you need to do is convert the character to an integer and output it, followed by a space...

myInteger = (int) myCharacter;

Should do that bit...


Well, this could be something like this:

int t_as_int = (int)'t';
int e_as_int = (int)'e';
int s_as_int = (int)'s';
int t_as_int = (int)'t';
.........


So, this is the basic process of converting a character to equivalent ascii number. As you need this case for text, so loop through your text and get the characters, and then convert it to ascii by above way.That's it, hope you can understand what to do now.


这篇关于我如何制作一个将文本转换为ascii数字的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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