|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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/ |
|
#3
|
|||
|
|||
|
thanks!
Cheers, that worked ok!
If i have any more trouble, ill be back ![]() Take care Seb |
|
#4
|
|||
|
|||
|
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... |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
Quote:
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. |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
Many thanks 4 your help ! This is getting me into it again :-)
Greets Hotkey |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > converting ascii to its decimal codes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|