473,899 Members | 3,071 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
80 5364
On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:
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;
}


I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #21
Alex Buell wrote:
On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:

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;
}

I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';

else ? }

--
Ian Collins.
May 21 '06 #22
Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 17:15:21 +1200, I waved a wand and this message
magically appeared from Ian Collins:
> I'd do something like this:
>
> short char2digit(char c)
> {
> if (c >= '0' && c <= '9')
> return c - '0';

else ?
> }
>


Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.


Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

--
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 #23
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.


Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.


Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #24
Alex Buell wrote:

On 20 May 2006 14:45:05 -0700, I waved a wand and this message
magically appeared from Gaijinco:
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;
}


I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}


The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.

--
pete
May 21 '06 #25
On Sun, 21 May 2006 06:03:32 GMT, I waved a wand and this message
magically appeared from pete:
short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}


The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.


Yes I know but I wasn't the OP ;o)

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #26
Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
> Oh yes. Don't need an else, but I'd put in a return -1 just in case
> input isn't valid.


Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.


Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}


No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.

--
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 #27
Keith Thompson wrote:

Alex Buell <al********@mun ted.org.uk> writes:
On Sun, 21 May 2006 05:46:33 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
> Oh yes. Don't need an else, but I'd put in a return -1 just in case
> input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.


Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}


No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.


int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}

--
pete
May 21 '06 #28
Keith Thompson said:
Martin Ambuhl <ma*****@earthl ink.net> 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;
return d;
}
?


#define CHAR2DIGIT(c) ((c)-'0')
#define DIGIT2CHAR(d) ((d)+'0')


I don't think I'd bother with the macros. Just using c-'0' or d+'0'
seems clear enough to me. YMMV.


In fact, this is a case where the code is actually clearer than the macro
names that purport to represent it. Why, for example, is there a number 2
in those names?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 21 '06 #29
On Sun, 21 May 2006 06:24:40 GMT, I waved a wand and this message
magically appeared from pete:
I'd also have the function return int rather than short.


int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}


Now that's I like to see!
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #30

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
2985
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
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10863
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10971
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10494
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...
0
9666
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8039
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
7201
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();...
1
4720
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
3
3317
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.