I am parsing an XML file using libxml2, and I开发者_如何学JAVA need to pull out a hex number from an xml attribute. Now, lib2xml doesn't give a null terminated string for the attribute, just pointers to the beginning and end of the attribute.
So, given .........FILL:BB0011AA;...............
(where dots indicate arbitrary
characters), and char* begin
pointing to the F in FILL
, and char* end
pointing
to the semi-colon, how can I efficiently pull out the hex number, WITHOUT making
a copy into a null terminated string?
Like so:
unsigned int value;
if(sscanf(begin, "FILL:%x;", &value) != 1)
printf("Parse error\n");
I think the above is safe even if the data at begin
isn't a string at all, i.e. if it doesn't contain a NUL
terminator. Since the %x
will abort if any non-hexadecimal character is found, there shouldn't be any risk of it running off into la-la land in memory. I can't come up with a case where it would run amok.
精彩评论