473,900 Members | 4,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char [0-9] to int [0-9]

Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?

Thanks.

May 21 '06 #1
80 5365

Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.
return d;
}


Tobias

May 21 '06 #2
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9}
I assume you mean '0' etc. The char type is an integer type and could
actually be set to those integral values. The '' are used with
character literals to differentiate that.
returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?


That's not portable, as it assumes the ASCII character set. The way to
do it is:

short char2digit(char c)
{
return c-'0';
}
Your variable d was unneeded, but not harmful.

Brian
May 21 '06 #3
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;


d = c - '0';

is the portable solution.


Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 21 '06 #4
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

In addition to the good advice already offered, don't forget to check
the range of c!

--
Ian Collins.
May 21 '06 #5
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;

d = c - '0';

is the portable solution.


Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).


What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

/Michael
May 21 '06 #6
Op Sat, 20 May 2006 22:20:04 GMT schreef Michael Brennan:
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.


Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).


What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?

/Michael


When you still use a Telex keyboard to program in C, you could be in
trouble ;-)
--
Coos
May 21 '06 #7
Michael Brennan wrote:
What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?


A conforming C implementation has to do whatever it takes
to make those characters appear to have consective values
in a C program.
In other words, a C programmer can assume that
they are consecutive and in order.

('9' - '0') is 9, and you can rely on that in a C program.

--
pete
May 21 '06 #8
Michael Brennan <br************ @gmail.com> writes:
Keith Thompson wrote:
to*******@hotma il.com writes:
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
d = c - '0';

is the portable solution.

Yes, and this true only because the standard specifically guarantees
that the digits '0' through '9' have representations that are
consecutive and in increasing order. Keep in mind that there is no
such guarantee for other characters, such as upper or lower case
letters (and there are character sets in which the letters are not
contiguous).


What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?


I suppose it would, but I don't think there are any character sets
currently in use in which the digits aren't contiguous.

The only current character sets I know of are EBCDIC, ASCII, and
various ASCII derivatives (including 8859-* and Unicode).

In EBCDIC, the letters aren't contiguous; if they had been, it's
possible that C would have required that as well.

Believe it nor not, some of C's requirements are actually based on
reality. 8-)}

(I just noticed this is cross-posted to comp.lang.c++. I presume C++
has the same rules in this area as C, but I'm not certain.
Cross-posts to comp.lang.c and comp.lang.c++ are usually a bad idea.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 21 '06 #9
Gaijinco wrote:
Is there an elegant way to do something like

// given a char {0,1,2,3,4,5,6, 7,8,9} returns it as a short
short char2digit(char c)
{
short d = c-48;
return d;
}

?


#define CHAR2DIGIT(c) ((c)-'0')
#define DIGIT2CHAR(d) ((d)+'0')
May 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3093
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
11
2986
by: Jason Curl | last post by:
Dear C people, C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char' leaving it to the implementation of the compiler. Has this changed for C99, or is it still the same? Thanks, Jason.
20
3585
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
889
by: Laura Creighton | last post by:
00:00 UTC 2007-09-02 to 00:00 UTC 2007-09-09 exactly. See www.pyweek.org PyconUK is happening. http://www.pyconuk.org/ 8th and 9th September. This means that those of us who generally do not see each other but are going to PyconUK could put together an entry and then sprint together on it before PyCon UK. There would be this terrible torment -- do I attend the con or get my game to work -- but it is still the best chance some of us...
12
9417
by: karthikbalaguru | last post by:
Hi, How is 'Int' Faster than 'Char' ? I think , 'Char' is small and so it should be easily & efficiently . Can someone here provide some info regarding this. Thanks and Regards, Karthik Balaguru
20
3242
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include <stdio.h> main(){ unsigned char c, temp; c = temp = 0; printf("the range of unsigned char is from %d to ", c);
4
3100
by: MimiMi | last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly! I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code: #include <time.h> #include <stdio.h> #define WEBBUF_SIZE 32768
12
1788
by: s0suk3 | last post by:
I've seen a lot of functions in the standard library that deal with characters, but a lot them return/take parameters of type int (which is usually the integer that represents the character code in the character set). I know that C automatically converts between int and char in those cases without problems (or could the singed/unsigned issue cause problems?), and that character constants have actually type int, but, what is the reason for...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3320
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.