473,899 Members | 2,918 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 5365
On Sun, 21 May 2006 15:31:06 GMT, I waved a wand and this message
magically appeared from Keith Thompson:
int char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else
return -1;
}


Why bother? There's a much better solution that someone has already
posted. It uses isdigit()
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 21 '06 #61
>>>>> "RH" == Richard Heathfield <in*****@invali d.invalid> writes:
I would never choose that solution...


RH> Neither would I. The consensus appears to be that the whole
RH> function is a complete waste of time.

I never intended it as a real solution.

RH> But Dale's code is still neat. :-)

Take some credit, I only debuged your code.
May 22 '06 #62
Richard Heathfield wrote:
Peter Jansson said:
Richard Heathfield wrote:

Er, yes, although I'd probably want to find a way to get rid of
the abs.

Come to think of it, I can see a really easy way to get rid of
the whole function. :-)


Would you like to share that "easy way" with us?


Gladly. Instead of writing:

n = char_to_int(ch) ;

write:

n = ch - '0';

And indeed that's what I do in my own code. It may be objected
that recent versions of char_to_int have range checks, and return
-1 if an out-of-range value is passed. But this means that you
have to do something like: ... snip ...


Provided n and ch are unsigned, then the test is simple:

if ((n = ch - '0') > 9) barfloudly();

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
May 22 '06 #63
pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:

Keith Thompson said:
Martin Ambuhl <ma*****@earthl ink.net> writes: #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?


atoi
a2i


What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

Richard
May 22 '06 #64
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:

Keith Thompson said:

> Martin Ambuhl <ma*****@earthl ink.net> writes:

>> #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?


atoi
a2i


What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?


I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.

--
pete
May 22 '06 #65
pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Heathfield wrote:
>
> Keith Thompson said:
>
> > Martin Ambuhl <ma*****@earthl ink.net> writes:

> >> #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?

atoi
a2i


What does a-twee-i have to do with atoi? Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?


I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.


Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT? Was it an inferior version? Will we get a
DIGIT3CHAR at some point?

Richard
May 22 '06 #66
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:

> Richard Heathfield wrote:
> >
> > Keith Thompson said:
> >
> > > Martin Ambuhl <ma*****@earthl ink.net> writes:
>
> > >> #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?
>
> atoi
> a2i

What does a-twee-i have to do with atoi?
Why is the first macro called
CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?


I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.


Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT?


If there was a way that a char could win a digit,
and I wanted to determine if that had happened,
I might write CHAR1DIGIT instead of CHARWONDIGIT.

--
pete
May 22 '06 #67
pete wrote:

Richard Bos wrote:

pete <pf*****@mindsp ring.com> wrote:
Richard Bos wrote:
>
> pete <pf*****@mindsp ring.com> wrote:
>
> > Richard Heathfield wrote:
> > >
> > > Keith Thompson said:
> > >
> > > > Martin Ambuhl <ma*****@earthl ink.net> writes:
> >
> > > >> #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?
> >
> > atoi
> > a2i
>
> What does a-twee-i have to do with atoi?
> Why is the first macro called
> CHAR-zwei-DIGIT? Why is the second called DIGIT-deux-CHAR?

I don't know.

I don't take languages that I don't know,
into consideration, when I write.
I'm American,
so I only know some of the American dialects of English.


Ok, let's try again, for the nil-lingual:

What happened to CHAR1DIGIT?


If there was a way that a char could win a digit,
and I wanted to determine if that had happened,
I might write CHAR1DIGIT instead of CHARWONDIGIT.


I think now I'm getting a little silly.

My point is that I've seen Martin Ambuhl's
naming convention in code before, and I've used it,
and when I was asked what my list_x2x function did,
I was asked about what was converted "to" what.

--
pete
May 22 '06 #68
["Followup-To:" header set to comp.lang.c.]
What if there was a system where the digits '0' through '9' is not
continuous? Would the standard translate them to a continuous
representation?


This is what the Standard has to say about the topic:

In both the source and execution basic character sets,
the value of each character after 0 in the above list of
decimal digits shall be one greater than the value of
the previous.

What I don't understand is why the consecutivity of 0 thru 9 is
required from the *source* character set as well. In case source
and execution set differ, won't the compiler have to translate
all character constants and string literals anyway?

robert
May 22 '06 #69
Richard Bos wrote:
> Martin Ambuhl <ma*****@earthl ink.net> writes:
>> #define CHAR2DIGIT(c) ((c)-'0')
Why is the first macro called CHAR-zwei-DIGIT?


maybe he thought it sounded better than BUCHSTABEZWEIST ELLE.

if one doesn't know english, who would CHARTODIGIT be any more clear?

May 22 '06 #70

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
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
11276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10976
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
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...
0
9671
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...
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
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
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.