Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
ascii numbers to text
text to ascii numbers
input: test case
output: 116 101 115 116 032 099 097 115 101
Posted
Comments
PIEBALDconsult 14-Dec-13 9:35am    
That sounds like homework. Break the problem down into smaller tasks and work on one at time.
Peter Leow 14-Dec-13 9:51am    
What have you tried?

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])
 
Share this answer
 
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:
C++
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...
C++
myInteger = (int) myCharacter;
Should do that bit...
 
Share this answer
 
Comments
Member 10466311 14-Dec-13 10:09am    
I just want some ideas for this project
OriginalGriff 14-Dec-13 10:14am    
What ideas do you need?
PIEBALDconsult 14-Dec-13 11:42am    
It's even asimpler than that -- especially accepting the input as a parameter.
OriginalGriff 14-Dec-13 11:44am    
Judging by his need for "ideas" after that bunch of rather heavy hints I don't think he is quite ready for a computing career yet... :sigh:
PIEBALDconsult 14-Dec-13 12:13pm    
At least it gave me an excuse to look at some of the C compilers I have available.
Well, this could be something like this:
C++
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.
 
Share this answer
 
Comments
PIEBALDconsult 14-Dec-13 14:37pm    
No converting or even casting is necessary.
ridoy 14-Dec-13 14:46pm    
For which you downvoted, right? OP asks for some ideas for the work and i only suggests him a way. Definitely this is one of the way of solution, not the only one.
PIEBALDconsult 14-Dec-13 14:54pm    
I see a 3. Is a 3 a downvote now? Sounds like the car dealer we bought from recently saying that anything less than the top rating was a failure. I told him that anything less than us coming back is a failure.
ridoy 14-Dec-13 14:59pm    
I see it as 2 first, and then found no it's a 3 and surprisingly found it is down-voted. I have never seen a 3 as down-vote before. Seems CP doesn't like anything rather than an up-vote, :)
PIEBALDconsult 14-Dec-13 15:02pm    
Huh, maybe it will change its mind again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900