8 Replies - 7762 Views - Last Post: 11 November 2010 - 04:45 AM Rate Topic: -----

#1 livin4th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 10

C Code to convert Ascii to integer

Posted 10 November 2010 - 08:31 AM

Hi,
I am just learning a c course all by myself from the net and need help with 1 issue. I have to use a tool and write a program in it where we need to take 10 integer inputs from a file and sort them using qsort. Now again, I am totally new to c coding. pointers and type casting are totally new to me. The problem is I am able to read the file and save the contents in an array. But when I print the array, I get ascii values of integers and not the integers I saved to array. Is there a way I can save the integers in their actual value in the array??

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C Code to convert Ascii to integer

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 08:33 AM

You need to show the code you have. Post it in code tags, as shown below:

:code:
Was This Post Helpful? 0
  • +
  • -

#3 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 08:35 AM

1) at least in what i know, no number gets an ASCII value, they are simply converted from base2 in base10.
2)if you're a beginner i advice you not to get dirty with qsort, it can get messy (it's not like a expert thing, it's just hard for a beginner to really understand what's happening there)
3) i'm preety sure the thing at point 1) is right, ASCII is for letters,special characters, and this type of stuff, well, back in the days they needed a way to "write" the letter a, for example, in binary.So they got a code.(i guess :) )

PS: I just googled the thing and yep :) i'm wrong with the stuff at point 1) :) sorry dude

This post has been edited by Patrunjel: 10 November 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 livin4th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 10

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 08:43 AM

View PostJackOfAllTrades, on 10 November 2010 - 07:33 AM, said:

You need to show the code you have. Post it in code tags, as shown below:


/*This code is a basic code for reading 1 integer and displaying it. I need to extend it to many integers and save them in array befor passing to qsort.*/
#include "stdio.h"

int main() 
{
int i;
i=READ(4);  // READ is an inbuilt function in the tool I mentioned. It reads data from another specific terminal.The terminal has integers. So it reads integers.
printf("%d",i);
return 0;
}



MOD EDIT: CODE tags, NOT QUOTE tags!

:code:

This post has been edited by JackOfAllTrades: 10 November 2010 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 08:43 AM

Here's the ASCII table. That's a good starting point.
Was This Post Helpful? 0
  • +
  • -

#6 livin4th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 10

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 08:50 AM

View PostJackOfAllTrades, on 10 November 2010 - 07:43 AM, said:

Here's the ASCII table. That's a good starting point.



Hi thanks for the link. I have already looked up for the table. I need help to figure out how to convert the ascii value back to integer. The code I posted, when given an input 1 in terminal prints 49 on display. (I figured 49 is ascii for 1!!). So I want to convert this 49 back to 1 and display it. I need to do this for numbers greater than 10 too. But thats the next step.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 09:04 AM

I want you to actually think, OK? No spoon-feeding, use your brain.

So you know that 49 is ASCII for the character '1', 50 for '2', etc. USE THAT KNOWLEDGE to come up with the VERY SIMPLE ARITHMETIC ALGORITHM for what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#8 livin4th  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 10

Re: C Code to convert Ascii to integer

Posted 10 November 2010 - 07:00 PM

View PostJackOfAllTrades, on 10 November 2010 - 08:04 AM, said:

I want you to actually think, OK? No spoon-feeding, use your brain.

So you know that 49 is ASCII for the character '1', 50 for '2', etc. USE THAT KNOWLEDGE to come up with the VERY SIMPLE ARITHMETIC ALGORITHM for what you're trying to do.



Ya I wrote the code and it works. Now The next part is reading multiple lines of data using gets. I know we can read 1 line at a time using gets. How do I read line by line?
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: C Code to convert Ascii to integer

Posted 11 November 2010 - 04:45 AM

A loop. Anytime you need to do something repetitively in a programming language, you use a loop. Example here in this blog entry. If you're using a FILE *, rather than stdin, then use that FILE * variable instead of stdin.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1