LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   String representation of octal to int (https://www.linuxquestions.org/questions/programming-9/string-representation-of-octal-to-int-544379/)

ManuPeng 04-08-2007 05:37 PM

String representation of octal to int
 
Hi,

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.

Any ideas? .toInt() returns 0...

Manu

ManuPeng 04-08-2007 06:07 PM

Done
 
You guys take too long, did you get drunk during the weekend? :-P

sscanf does it:
Code:

QString test("040");
int oct;
(void) sscanf( test.ascii(), "%o", &oct );
// i = 32  (32 dec = 040 octal)


graemef 04-08-2007 10:07 PM

I don't understand why you want to convert the octal to an int. Just do a replace of the string \\040
Code:

QString data("US\\040of\\40A");
data.replace(QRegExp("\\040"), " ");


ManuPeng 04-09-2007 06:26 AM

Because
 
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 :-)

graemef 04-09-2007 07:24 AM

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);


ManuPeng 04-09-2007 07:40 AM

Well no...
 
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 "(".

ManuPeng 04-09-2007 07:42 AM

Oh sorry
 
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...

Manu

ManuPeng 04-09-2007 07:45 AM

It works
 
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.

Thanks a bunch,
Manu

graemef 04-09-2007 11:02 AM

I'm glad that it worked out :)


All times are GMT -5. The time now is 01:33 PM.