ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In reality, I'm starting with an array of bytes (unsigned chars), I require to convert them to hex (which seems fine here), and then convert back. I'm not sure if its just late but I'm struggling to copy the vector of bytes to an array of bytes, also I'm wondering if there's a better way without the intermediary vector...? Any help cheers!
If I return the vector I can print out the values, but when I put to an array or recast I get nothing...?
When you print the vector, the runtime is converting the byte values inside the vector to a decimal string. If you print a byte array directly, it will be interpreted as a C string. Since the 0 byte at the front indicates end-of-string, none of the other bytes will ever be printed (and many of them are not printable characters anyway).
Well, I was waiting till tomorrow to chime in because I largely only had to say the following.
I compiled the original program. It converts to hex. The second part doesn't work. I observed it in the debugger and the converted output data is NOT 00, 01, 02, ... it's more like random bad data. So the shex2bytes() function does need to be fixed. A point I was also going to make was to state that one should realize that hex-ascii, if that's what it sees the input as, is actually two characters in an array and therefore one has to take like { 0x30, 0x31 } to be interpreted as "01", the 0x30 representing the upper nibble of the binary result. Not sure if that's part of the problem, but clearly sure that something further is wrong because the output data was no where near the correct values, again it was purely random stuff. I just don't have the time to look further today, and will look at it in the morning.
I also wanted to encourage the OP to post more updates if they go further and have some more success. And I also was going to suggest that the OP use a debugger, like GDB to break and observe the variables directly in memory, because that, to me, is definitive. So if you happen to "print" incorrectly, or use the wrong function to get your output, knowing that you did the conversion fully correctly is helpful so you know where to concentrate your efforts. My take is that the conversion is not working, but haven't looked enough on my part to understand yet why that is the case.
That works. I suppose a full test would be what you've done before, which is to take an array from 0x00 through 0xFF and convert that from pure binary to a HEX string using your toHex() function, validate that you did it all, and then convert the HEX string back to binary bytes and validate that this worked. However what you wrote here does work for the test pattern you put in, "LINUX", and I'm fairly sure it will just work for all data. As ntubski points out, many of the characters in the full ASCII set of available patterns will be non-printable. Therefore I look at things like that in the debugger. Otherwise you'd have to use a convert function not of your own manufacture to make then ASCII printable.
I'm not going for the credit here, hence why I've left the original function names as found.
I'd have done this in a very manual way, which would have probably been quicker (to do), but was looking for common practice when using C++. As I was thinking today, I've spent too long in the world of Lua and PHP and my C / C++ knowledge has gone to mush, a visit to my parents for my old books is in order...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.