LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2007, 05:37 PM   #1
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Rep: Reputation: 15
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
 
Old 04-08-2007, 06:07 PM   #2
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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)
 
Old 04-08-2007, 10:07 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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"), " ");
 
Old 04-09-2007, 06:26 AM   #4
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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 :-)

Last edited by ManuPeng; 04-09-2007 at 06:28 AM.
 
Old 04-09-2007, 07:24 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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);
 
Old 04-09-2007, 07:40 AM   #6
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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 "(".
 
Old 04-09-2007, 07:42 AM   #7
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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
 
Old 04-09-2007, 07:45 AM   #8
ManuPeng
Member
 
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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
 
Old 04-09-2007, 11:02 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I'm glad that it worked out
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
Printing the octal representation of a character. smoothdogg00 Programming 1 03-02-2006 10:44 PM
string to int BadTaste Programming 11 08-18-2004 10:23 AM
string to int in C h/w Programming 2 12-05-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration