- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
YES
Admin
Why not make it just go up to F and define F as 11?
Marty
Admin
theoretical perfectionists that have nothing better to do than nit-pick == thedailywtf
Admin
Admin
http://en.wikipedia.org/wiki/Casting
Admin
Admin
Ah, so you've seen a system that translates ASCII to EBCDIC (as well as it can). In that case, consider a CGI implementation, which obviously has to interact with the rest of the world but doesn't have to expose the scripts it runs.
I agree. This code is better written as something like:
int transform(unsigned char c) { char charset[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; return isdigit(c) ? c - '0' : 10 + (strchr(charset, c) - c) / 2; }
Admin
Okay, umm... Does Latin matter? Please discuss with ludus (my captcha).
Admin
The code was written by a newb, and anyone who insists that the code is fine is also a newb. You'll understand, in five to ten lawsuits time, when you've realised the err of your ways and decided to read the C standard (and the Objective-C standard).
Admin
"Calling it atoi() might be a bit confusing to those used to C's atoi() ..."
Yes, and the developer has agreed to write code in C. That means the developer should follow the rules of C.
"... but if anything it's C's atoi() that's named confusingly"
Yes, but the developer has agreed to write code in C. That means the developer should follow the rules of C.
"... why not call that one stoi() since it clearly works on a string, while this one doesn't?"
Presumably, atoi was named with an 'a' instead of an 's' because it operated on an "array" of decimal digits which isn't necessarily a string. That's just a guess. Since there's already an strtod, why not typedef FILE* file; typedef tss_dtor_t destructor; typedef unsigned int uint; typedef uint32_t uint32; while we discuss this?
Admin
Admin
and you
Kids, it was named atoi because its accepting ASCII characters as an input.
Admin
Admin
I remember someone writing a function very similar to this, and everyone wondering why it was running unexpectedly slow. I mean really slow, even if you passed '0' to this one where strchr wouldn't be called. Why did you write "char charset[]" and not "char* charset"? Because the one you wrote means that an array of 53 bytes has to be initialised with the contents of the string, every time the function is called. Had you used char* charset, the string would be forever stored in a static array of const char.
Then there's of course two awful bugs. For one, it doesn't compile because subtracting c from the result of charset gives a pointer which cannot be divided by two. Second, if you change c to charset, and c is not a letter, strchr will return a null pointer and the function will have undefined behaviour, most likely returning total rubbish.
Admin
Guess what: There's no specification for the method, so you can't say that calling it with for example '?' or 0 as an argument is an error or not.
However, anyone with a tiny bit of experience in writing usable code will see that someone has to check whether inputs are correct, and that the rather complicated rules which inputs are correct must match between the caller and the callee, and that since the method checks all these cases anyway, it is by far the best solution if the method does the checking itself and informs the caller if the argument is wrong.
That way we save lots of work on the caller side, and make sure automatically that any checks will be performed in a consistent way. So a reasonable specification would define what inputs are accepted as valid numerical values, what inputs are not, and what would be returned for inputs that are not numerical values.
Admin
Admin
This is for hex and works because it is one char at a time. There must be an atoi somewhere for sequences that does the product from the end to the beginning into an int.
Admin
This is for hex and works because it is one char at a time. There must be an atoi somewhere for sequences that does the product from the end to the beginning into an int.
Admin
Additionally, supporting upto Z could be because why not or because some people use base 36 in formats that must support string representations of numbers such as XML for a poor man's compression.
I could question this code, the error handling or lack thereof, the function name and wonder that surely there must be some library function to do the same but I wouldn't mock it that harshly without seeing the context.
Admin
Well, no, it is would not be hextricontesimal either. If we attempted to use the Greek root system for base36 it would be triacontaheximal. But of course such a word is not used, nobody has proposed naming base36. However, the equivalent "triacontahexadic" is a perfectly valid word in modern Greek. Ok, not many uses since base36 is not popular (yet?)!
Admin
atoi doesn't know when it reaches the end of the array. To the contrary, it knows nothing about the size of the array, or even if it is an array. atoi parses a sequence of decimal-digit characters, and stops parsing when it reaches a non-decimal-digit character. It's not specifically a string function, because it can operate on non-strings.
ps. I hope you guys aren't using your real names, here...
Admin
If you bother to read the Objective C specification, you'll realise that Objective C is a strict superset of C. This means that atoi() in Objective C is required to behave as it does in C.
Yes. Read the Objective C language specification, and you'll realise that Objective C is a strict superset of C.
No. If you bother to read the specification, you'll find that atoi operates on a "sequence" of decimal digit characters terminated by a non-decimal-digit character. The sequence doesn't have to be in an array, nor does it have to be "terminated by a NUL".
Under the same circumstances where the sequence of decimal digit characters is terminated by some other non-decimal-digit character.
Of course not, because it's become clear that you don't like to read/research. Anyone else would have googled those keywords to develop an understanding.
Admin
Slow using which compiler? I could build a compiler that produces awful machine code... Does that help you? No. Stop gibbering. C code doesn't have speed; That's an aspect introduced by implementations (eg. my suboptimal compiler). Most common C compilers will optimise things such as strlen("hello") AOT, and they can do something fairly similar in this case.
Stop guessing. Get your meaningful, actual problem solved. If it's too slow, profile it and use the results to determine where to optimise.
Okay. Let us run through these one by one.
No. Subtracting one pointer from another pointer of the same type results in a ptrdiff_t, which is not a pointer, but an integer capable of representing the difference between two pointers.
An integer cannot be divided by two? You're cooked!
Yes. I knew of this beforehand.
I reiterate: Something like the code I originally posted would do. Please read and comprehend more carefully, in the future.