Re: Read hex string to a buf



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? :)
.



Relevant Pages

  • Re: Read hex string to a buf
    ... Not critical in a standalone program, ... The type of the variable you are assigning an expression to has no effect on how the expression is evaluated, only on what conversion occurs during the assignment. ... I would want a tighter specification for what was required on systems with larger char types. ...
    (comp.lang.c)
  • Re: char Problem
    ... | Manoj wrote: ... |> char *p; ... | Practically, p contains garbage. ... I thought globals were default initialised? ...
    (alt.comp.lang.learn.c-cpp)