473,883 Members | 1,610 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 5361

"tedu" <tu@zeitbombe.o rg> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
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?


If you want to chart an odigit. Maybe char_to_digit or charToDigit or
something...
May 22 '06 #71
Duane Hebert wrote:
"tedu" <tu@zeitbombe.o rg> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
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?


If you want to chart an odigit. Maybe char_to_digit or charToDigit or
something...


For those who don't know English, does 'char' or 'digit' mean the same
thing as for those who do know it? Then would it be possible that when
they read the name, instead of thinking 'zwei', they do think 'two',
simply because they are in the "English" mode? Just a thought...
May 22 '06 #72
Alf P. Steinbach wrote:
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:

template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };

...
int array[IntFromDigit<'7 '>::value];

If that conversion were ever required, it would be in template code, so
a template solution is very fitting.


Just use:

(digit - '0')

and be done with it.
May 22 '06 #73
Victor Bazarov opined:
Duane Hebert wrote:
"tedu" <tu@zeitbombe.o rg> wrote in message
news:11******** **************@ y43g2000cwc.goo glegroups.com.. .
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?
If you want to chart an odigit. Maybe char_to_digit or charToDigit
or something...


For those who don't know English, does 'char' or 'digit' mean the
same thing as for those who do know it?


For those who don't know English, both mean absolutely nothing at all
(unless they speak Latin, or their native language is of Romance
descent, in which case they may get the "digit").
Then would it be possible that when they read the name, instead of
thinking 'zwei', they do think 'two', simply because they are in the
"English" mode? Just a thought...


Using "2" for "to" became commonplace in software circles, so I doubt
it's misleading. OTH, I still mentally read out all the numbers (apart
from char2digit variety) in my native language, even in English mode
(and I'm in English mode for at least 75% of my waking hours). YMMV.

--
The sight of death frightens them [Earthers].
-- Kras the Klingon, "Friday's Child", stardate 3497.2

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 22 '06 #74
On Mon, 22 May 2006 16:17:30 -0400, I waved a wand and this message
magically appeared from Victor Bazarov:
If you want to chart an odigit. Maybe char_to_digit or charToDigit
or something...


For those who don't know English, does 'char' or 'digit' mean the same
thing as for those who do know it? Then would it be possible that
when they read the name, instead of thinking 'zwei', they do think
'two', simply because they are in the "English" mode? Just a
thought...


In English, my fingers are also 'digits'.

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

Take a nap, it saves lives.
May 22 '06 #75
* Walter Bright:
Alf P. Steinbach wrote:
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:

template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };

...
int array[IntFromDigit<'7 '>::value];

If that conversion were ever required, it would be in template code,
so a template solution is very fitting.


Just use:

(digit - '0')

and be done with it.


What I wrote was, just use the number 7. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '06 #76
Vladimir Oka opined:
Victor Bazarov opined:
For those who don't know English, does 'char' or 'digit' mean the
same thing as for those who do know it?


For those who don't know English, both mean absolutely nothing at all
(unless they speak Latin, or their native language is of Romance
descent, in which case they may get the "digit").


Not to mention that, out of context, "char" can be construed as a
variant of "charcoal", especially if, in a different life, one was
involved in coal combustion. ;-)

--
Change is the essential process of all existence.
-- Spock, "Let That Be Your Last Battlefield", stardate 5730.2

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 23 '06 #77
Vladimir Oka wrote:
Vladimir Oka opined:
Victor Bazarov opined:
For those who don't know English, does 'char' or 'digit' mean the
same thing as for those who do know it?


For those who don't know English, both mean absolutely nothing at all
(unless they speak Latin, or their native language is of Romance
descent, in which case they may get the "digit").


Not to mention that, out of context, "char" can be construed as a
variant of "charcoal", especially if, in a different life, one was
involved in coal combustion. ;-)


Yeah, yeah... And "char2digit " means "Zwei parts ashes, ein part finger"
(analogous to H2O), right?
May 23 '06 #78

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

?

Thanks.


int char_to_digit(c onst char & chardigit)
{
static const int zeroindex = static_cast<int >('0');

int digit = (static_cast<in t>(chardigit) - zeroindex);

if ((digit > 9) || (digit < 0)) return -1; // or throw an exception

return digit;
};

May 23 '06 #79
oops, rather

short char_to_digit(c onst char & chardigit)
{
static const short zeroindex = static_cast<sho rt>('0');

short digit = (static_cast<sh ort>(chardigit) - zeroindex);

if ((digit > 9) || (digit < 0)) return -1; // or throw an exception
else return digit;
};

May 23 '06 #80

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
2983
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
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...
1
10836
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
10407
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
9564
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
7960
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
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.