M
Mark McIntyre
It is, however, true in both ASCII and EBCDIC.
Do you know anyone using a different character set?
Only a couple of billion of them.
"All the world's a Vax" as someone once said.
It is, however, true in both ASCII and EBCDIC.
Do you know anyone using a different character set?
The said:.... snip ...char *p
unsigned int v;
int d;
.....
p = &instring[0];
v = 0;
while ((d = unhexify(*p)) >= 0) {
v = 16 * v + d;
++p;
}
/* desired result in v */
Calling a function for each char is another brake. I had not used
pointers because I'm not sure that the OP is ready to understund
pointer arithmetc yet and any good compiler should create even the
same code.
By that, the separate p++ can be another brake as many real CPU
can do *p++ in an single instruction instead of *p and some times
later p += 1.
That combination is a trap. It would allow the pointer to be
advanced past the known area of instring, which may or may not be
legitimate.
And that is really wrong. As the standard allows to build the address
of the first member directly behind the array.
Dan said:It has the same bug that I deliberately left unfixed in my
version, posted to the original thread ;-)
Not if one of the objectives is to examine the termination
character. It is an extremely minor point anyhow, and closer to a
style criterion than anything else.
In said:That's the most dangerous kind of typos: the ones that aren't caught
by the compiler and are hard to find, even when you *know* they are
there (and yes, I know that you already knew that .
In said:'A' exists in both the basic source and basic execution character sets.
^The trivial, assumption free approach is to define the hex digits
yourself:
int hex2dec(char c)
{
char *hexdigs = "01234567890ABCDEF";
return (int) (p - hexdigs);char *p = strchr(hexdigs, toupper((unsigned char)c));
if (p != NULL)
return p - hexdigs;
else
return -1;
}
Trivia quiz: find the bug in this code.
In said:printf (A is %i\n", hex2dec ('A'));
should result in
A is 11
'0' surely??Johan Aurer said:Which one? The c == 0 bug?
'0' surely??
Actually it will return the actual represenation + 1 for all the
digits from 'A' to 'F'.
Change to :
char *hexdigs = "0123456789ABCDEF";
Oh that's quite Ok. I understand that strchr returns a pointerIrrwahn Grausewitz said:No. Hint: what does strchr return if c == 0?
Beg your pardon. You had already posted the answer. I was too fast atIrrwahn Grausewitz said:No. Hint: what does strchr return if c == 0?
That was the typo, not the bug.
^printf (A is %i\n", hex2dec ('A'));
printf ("A is %i\n", hex2dec ('A'));
Comprendo
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.