开发者

c : parse hex number inside long string

开发者 https://www.devze.com 2023-03-01 13:15 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消