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.
Anyone knows a good way to make an int out of the escape sequence of an octal value?
Say my QString looks like this: "US\\040of\\40A"
\040 is the octal representation of the <space> character (" ").
I want to code a loop that will replace the "\\040" with " ", or whatever escape sequence it finds with the corresponding character, but for that, I need a way to make an int out of the "\\040" string chunk.
Trust me, I thought of that, but I have no guarantee that my escape sequence will be "\\040", it could be "\\045" or whatever you like, I just don't know.
So instead of writing fragile code, I prefer to just go for the whole nine yards and have a stable thing going on.
I'm a software quality assurance specialist in real life, so my eye is trained to identify potential weaknesses. If my escape string has a default, the rest of the program that's based on it is useless.
Manu
Ps: Anyway, I have my code ready, it could replace any octal escape sequence with the corresponding character and I sleep better at night :-)
Sorry I missed the bit about "or whatever escape sequence it finds". Also you've got it working so why fix it?...but I would suggest, if only for future reference that you could us the QString member function toInt().
Code:
QString test("O40");
bool ok;
int dec = test.toInt(&ok,8);
You code will return 40 in decimal, but I want 40 in octal, hence 32 in decimal.
See the trouble I went through now?
With the sscanf code I've written, when I feed the string "040" to sscanf and let it know it's an octal, as in the %o argument, it returns 32 in the variable oct. 32 is the decimal code for <space>, your 40 would be the code for "(".
I just noticed you were passing 8 as a base to .toInt(). So my comments don't apply.
Are you sure it works though, because I remember fooling around with it yesterday but it never worked... I could have been passing "\\040" as a QString though, let me try...
You were right, all apologies, I was passing the entire escape sequence when trying it yesterday. Your code is much shorter and much more efficient, so even though I've implemented something on my own, I'll still go back and replace it with your solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.