SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old December 19th, 2002, 05:17 AM
seb_sikora seb_sikora is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 seb_sikora User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
converting ascii to its decimal codes

Hi

Ive been looking for some c code to convert an ascii character, to its decimal code. I cant find anything in any of the books i have, and havent had any luck with google.

In Quick Basic, there is a simple command to do this, just the command ASC using the structure

ASC("A") gives the decimal value of A, 65.

Any help would be much appreciated.

Thanks

Seb

Reply With Quote
  #2  
Old December 19th, 2002, 09:29 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
You don't need a function to get the ASCII value of a character. The expression 'A' simply evaluates to 65. Hence, to print 'B', you could use 'A' + 1:

Code:
print("%c\n", 'A' + 1);


You can even do this:

Code:
print("%d\n", 'Z' - 'A');


which will print 25.

Hope this helps.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old December 19th, 2002, 10:29 AM
seb_sikora seb_sikora is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 seb_sikora User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks!

Cheers, that worked ok!

If i have any more trouble, ill be back


Take care

Seb

Reply With Quote
  #4  
Old December 19th, 2002, 10:33 AM
amitchd amitchd is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 1 amitchd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ascii val

Getting an ascii code of a character is quite easy in c++. There are many ways... Suppose if you want to print the ascii code you may just do as follows..

Code:
char a='B';
printf("%c=%i",a,a);

so the output would be B=66.

But.. if you want to get the ascii value in some variable

Code:
char a='B';
int as=a;

so 66 is stored to as ... this is implicit conversion..

you may also implement this with a function
Code:
int asc_ii(char a)
{    int k=a; return a;
}
]

Hope this helps...

Reply With Quote
  #5  
Old February 13th, 2003, 04:57 AM
Hotkey Hotkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 Hotkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hmmm, i've read this threat to find out how i can get the ascii code for a character just to find out that it doesn't works for me completely.
So far i used the following method to reveal the ascii code :

Code:
char asc='G';
int code=asc;


I need this to read out a binary file. Now i got the Byte "AA" - which should be resolved to the ascii code 170 (dezimal).
But my above code gives me a (-86) - so i can't resolve the Hexadezimal value with this one.

Can anyone help me out with this ?

Thanks in advance

Hotkey

Reply With Quote
  #6  
Old February 13th, 2003, 09:41 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
Quote:
Originally posted by Hotkey
Hmmm, i've read this threat to find out how i can get the ascii code for a character just to find out that it doesn't works for me completely.
So far i used the following method to reveal the ascii code :

Code:
char asc='G';
int code=asc;


I need this to read out a binary file. Now i got the Byte "AA" - which should be resolved to the ascii code 170 (dezimal).
But my above code gives me a (-86) - so i can't resolve the Hexadezimal value with this one.

Can anyone help me out with this ?

Thanks in advance

Hotkey


Obtaining the ASCII code for a given character is one thing, but converting an ASCII representation of a hex value to the value being represented is a different matter altogether.

What you need is the hexadecimal equivalent of atoi(). Fortunately, I've already written one:

Code:
// uint is unsigned int
// ulong is unsigned long

int hexval(char c)
{    
   int n;
   
   if ((c>='0') && (c<='9'))
      n = c - '0';
   else if ((c>='A') && (c<='F'))
      n = c - 'A' + 10;
   else if ((c>='a') && (c<='f'))
      n = c - 'a' + 10;
   else 
      n = 0;
   return n;
}

                                      
uint htoui(char *s)
{
   char *cp; 
   uint n;
      
   n = 0;
   for (cp = s; (*cp != '\0') && ((*cp == ' ') || (*cp == '\t')); cp++);
   for ( ; (*cp != '\0') && isHex(*cp); cp++)
      n = ((n << 4) & 0xFFF0) + hexval(*cp);
   return n;
}


ulong htoul(char *s)
{
   char *cp; 
   ulong n;
      
   n = 0L;
   for (cp = s; (*cp != '\0') && ((*cp == ' ') || (*cp == '\t')); cp++);
   for ( ; (*cp != '\0') && isHex(*cp); cp++)
      n = ((n << 4) & 0xFFFFFFF0L) + hexval(*cp);
   return n;
}



That should help.

Reply With Quote
  #7  
Old February 13th, 2003, 09:44 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
Oops! I thought that I had highlighted the isHex() function too.

isHex() is analogous to isdigit().

Code:
int isHex(char c)
{
   return ( ((c>='0') && (c<='9')) || ((c>='A') && (c<='F')) || 
            ((c>='a') && (c<='f')) ) ? 1 : 0;
}


BTW, thanks for the "code" tag.

Reply With Quote
  #8  
Old February 14th, 2003, 05:38 PM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Heres a curve ball

what about
Code:

#include <stdio.h>
#include <stdlib.h>

int htoi(char *str){
  unsigned int x;
  sscanf(str,"%2X",&x);
  return (int) x;
}

int main(int argc, char **argv)
{
  printf("%d\n", htoi("AA"));
  return 0;
}


As they say don't try to reinvent the wheel.
__________________
--

ngibsonau

Reply With Quote
  #9  
Old February 17th, 2003, 03:28 AM
Hotkey Hotkey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 Hotkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Many thanks 4 your help ! This is getting me into it again :-)
Greets
Hotkey

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > converting ascii to its decimal codes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway