As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. I found this question by Googling for the same thing. I don't like the idea of calling sscanf or strtol since it feels like overkill. I wrote a quick function which does not validate that the text is indeed the hexadecimal presentation of a byte stream, but will handle odd number of hex digits:. For short strings, strtol , strtoll , and strtoimax will work just fine note that the third argument is the base to use in processing the string Apart from the excellent answers above I though I would write a C function that does not use any libraries and has some guards against bad strings.
So, for every 2 string characters we add the value to 1 data byte. That'll make it even more minimal requiring few libraries — Imtiaz Shakil Siddique. I'm not sure if you got downvoted because of Boost, but I think your answer is acceptable and should not have a negative score. For Qt alternative: stackoverflow. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked 6. Related Hot Network Questions. Question feed. There's no spaces between numbers in my string so I can't split it. Sorry, that link doesn't show my example I'm sorry, I guess I didn't make it clear that the length of the string is variable and can be way longer than that.
Here's another example:. The content you requested has been removed. Ask a question. Quick access. Search related threads. Remove From My Forums.
0コメント