Re: Read hex string to a buf



Keith Thompson wrote:

hex2dec.c:39: warning: array subscript has type 'char'

Whoops, I should have paid more attention; I didn't realize the
subscripts were of type char (which can be signed on some
implementations). The values you're using happen to be guaranteed to
be positive, but the compiler understandably didn't figure that out.

I'm not sure size_t is the best type to convert to, but it's not
unreasonable. Or you might declare "set" as an array of unsigned
char.

Yep. It's of type char as I'm using it as a simple character hash. Anyways, I
agree that just using an array of unsigned char rather than char is cleaner
and achieves the same goal in the end. Unfortunately, it's a 'choose your
battle' as I'm using the same technique further down and changing 'q' to
unsigned char means it's now time to fight with casting argv[1]. But this
is only 1 cast vs 4, so comparitively a lesser deal.

In reference to size_t, I've always used this in situations related to array
indexing as I have always been under the impression size_t is guaranteed
to be able to represent an index value. Of course, things wouldn't go so well
if one of the characters within that above set were actually a negative
value - then again, casting to int wouldn't help either.

(gdb) p /d (size_t)(char)129
$1 = 4294967169
(gdb) p /d (int)(char)129
$2 = -127

Although I did have direct control over the characters I used in the set,
still a better decision to use unsigned char, agreed.
.



Relevant Pages

  • Re: char *p = "longenough"; // Str255 spin off
    ... > Also, if I remember correctly, the first three characters for ``str'' is ... >> point I initially missed was that char and unsigned char are not ... applicable, in particular as basic types, and says "Even if the ...
    (comp.lang.c)
  • Re: clarification on character handling
    ... >> Windows character sets for English-speaking versions are largely based ... They have the top bit of the char set. ... characters set is all positive, there are plenty of well-used characters ... But if you use an array of unsigned char then you can't use strlen, ...
    (comp.std.c)
  • Re: chars and ints
    ... My understanding is that a type char ... Functions such as fgetcare used for reading in characters, ... have something different to indicate EOF. ... should use unsigned char rather than char when reading memory ...
    (comp.lang.c)
  • Re: delirious program, episode II
    ... char *f ... New fresh storage (type char *) passed as implicitly ... temp (not ...
    (comp.lang.c)
  • Strings and Characters
    ... function that has a preset array of characters defined and it returns ... char* readName; ... If I get the method above to work how would I convert name to type char ...
    (alt.comp.lang.learn.c-cpp)