473,883 Members | 1,730 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 5362
pete wrote:
Richard Heathfield wrote:

.... snip ...

Why, for example, is there a number 2 in those names?


atoi
a2i

I rhyme "atoi" with "patois", but most people don't.


Thus it should be written as "2u" :-)

--
"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 21 '06 #41
Alex Buell wrote:
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!


You like obfuscation then?

--
"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 21 '06 #42
pete said:
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


I don't see the connection.

Well, all right, I do really. I'm just determined not to.
--
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 #43
Dale Henderson said:
>> "RH" == Richard Heathfield <in*****@invali d.invalid> writes:

RH> int char_to_digit(i nt ch)
RH> {
RH> return (2 * (ch >= '0' && ch <='9') - 1) * (ch - '0');
RH> }

I think you mean

int char_to_digit(i nt ch)
{
return (2 * (ch >= '0' && ch <='9') - 1) * abs(ch - '0');
}


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. :-)
--
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 #44
Keith Thompson wrote:

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

And there's a reason why they aren't rooted in antiquity.
A hint is "Junior is eleven" but none of you here is likely
old enough to know what that means.
May 21 '06 #45
pete wrote:
-snip-
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


I don't know if it's been proposed before, but what about just:

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

Which I think is easy to read and only consists of a single line.
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 21 '06 #46
CBFalconer wrote:
Alex Buell wrote:
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!

You like obfuscation then?


Obfuscation is in the eye of the beholder.

--

Pete Becker
Roundhouse Consulting, Ltd.
May 21 '06 #47
Martin Jørgensen <un*********@sp am.jay.net> writes:
pete wrote:
-snip-
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


I don't know if it's been proposed before, but what about just:

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

Which I think is easy to read and only consists of a single line.


What exactly is the virtue of putting the whole thing on one line?

If you're determined to write dense code, you can always write:

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

Or you can write *clear* code:

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

or, if you prefer:

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

--
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 #48
>>>>> "RH" == Richard Heathfield <in*****@invali d.invalid> writes:

RH> Dale Henderson said:

int char_to_digit(i nt ch)
{
return (2 * (ch >= '0' && ch <='9')- 1) * abs(ch - '0');
}


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

Without the abs you could use:

int char_to_digit(i nt ch)
{
return (2 * (ch <='9')- 1) * (ch - '0');
}

RH> Come to think of it, I can see a really easy way to get rid of
RH> the whole function. :-)
RH> -- Richard Heathfield "Usenet is a strange place" - dmr
RH> 29/7/1999 http://www.cpax.org.uk email: rjh at above domain
RH> (but drop the www, obviously)
May 21 '06 #49
On 2006-05-21, CBFalconer <cb********@yah oo.com> wrote:
Alex Buell wrote:
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!


You like obfuscation then?

With a few parenthesis, that is easily understood:
return ((c >= '0') && (c <= '9')) ? (c - '0') : (-1);

Okay, not "easily", but much better, at least. Also, I just realized
that Alex had used '=>' as an operator.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.
May 21 '06 #50

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

Similar topics

20
3089
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
2984
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
3582
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
9416
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
3241
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
3099
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
9933
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
9781
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
11123
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...
0
10734
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...
0
7114
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
5794
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...
1
4606
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
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
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.