Re: Read hex string to a buf
- From: Christopher Layne <clayne@xxxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 02:17:31 -0800
Flash Gordon wrote:
const size_t minl = 2;
const size_t nibl = 4;
const char delim[2] = "0x";
const char set[2][16] = { "0123456789abcdef", "0123456789ABCDEF" };
unsigned char lookup[2][256];
Why are these globals? Not critical in a standalone program, but when it
gets put in to a library as this obviously would it starts becoming
annoying.
This is not library code. This is "<OP> how do I do X?" Here's some X example
code on how to do it.
Non-standard return from main. If there are multiple different failure
codes there is reason for non-standard values, but that is not the case
here.
return EXIT_FAILURE;
Alrighty.
This you corrected in a subsequent post, but just in case that gets missed:
for (i = sizeof set[0]; i--; ) {
I would for clarity use:
for (i = sizeof set[0]; i > 0; i--) {
Unfortunately that is wrong and not the same effect as what I wrote.
p[i / 2] = n;
As a matter of style I do not see the point of n here.
Most likely because I was choosing to stick with an integer for integer
operations and then assign the result to the 1 byte character value. This
could be argued to be needless pre-opt and that n is superfluous.
Well, that seems to work, although it does have an undocumented
assumption of an 8 bit char and I think it could misbehave on a 1s
complement or sign-magnitude system. Fine for anything the OP is likely
to come across unless getting in to embedded systems though where you do
get 16, 24 and even 32 bit chars.
Yep. Definitely assumes an 8-bit char. What's your entirely portable
solution? :)
.
- References:
- Read hex string to a buf
- From: cppbeginner
- Re: Read hex string to a buf
- From: Christopher Layne
- Re: Read hex string to a buf
- From: Flash Gordon
- Read hex string to a buf
- Prev by Date: Re: OT: Google's latest dungheap
- Next by Date: Re: OT: Google's latest dungheap
- Previous by thread: Re: Read hex string to a buf
- Next by thread: inside scanf
- Index(es):
Relevant Pages
|