LinuxQuestions.org
Visit Jeremy's Blog.
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 01-02-2004, 08:59 PM   #1
CamelofCamelot
Member
 
Registered: Aug 2003
Distribution: Slackware, ClusterKnoppix, Gentoo
Posts: 85

Rep: Reputation: 15
allegro + hex integers


How, in C++, do I save a hex value to an integer?

I would like to read in hex values from a file opened w/ ios::hex|ios::in. ios::hex is correct, neh?

Also, what options does g++ need to compile an allegro program.
 
Old 01-03-2004, 02:52 AM   #2
CamelofCamelot
Member
 
Registered: Aug 2003
Distribution: Slackware, ClusterKnoppix, Gentoo
Posts: 85

Original Poster
Rep: Reputation: 15
Nevermind on the allegro thing, I managed to find the answer (search: -lalleg), which was close enough to the real thing

but how does one save hex values to an integer?
 
Old 01-03-2004, 08:29 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Code:
#include <iostream>
#include <sstream>

int main()
{
     int intvalue;
     string hexstr = "FF";
     istringstream istr(hexstr);

     istr.setf(ios::hex, ios::basefield);
     istr >> intvalue;

     std::cout << hexstr << " in decimal is: " << intvalue << std::endl;
     return 0;
}
 
Old 01-03-2004, 12:34 PM   #4
CamelofCamelot
Member
 
Registered: Aug 2003
Distribution: Slackware, ClusterKnoppix, Gentoo
Posts: 85

Original Poster
Rep: Reputation: 15
That appears to be just formatting a decimal int as hex for cout. I'd like to be able to do something like:
for(int i=0; i<FF; i++) or something

OH! is it

for(int i=0x00; i<0xFF; i++) ?? would that work?

Nevermind, I found a way to do it w/out using hex.
 
Old 01-04-2004, 09:56 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Quote:
Originally posted by CamelofCamelot
That appears to be just formatting a decimal int as hex for cout.
No, it's not.
It is reading as string formatted as hex into a integer.
Have a look at the code. The integer intvalue gets the value 255 as translated from the string hexstr which is "FF". After that it is printed with cout.

Quote:
Originally posted by CamelofCamelot
OH! is it

for(int i=0x00; i<0xFF; i++) ?? would that work?
Yes, that should work.

Last edited by Hko; 01-04-2004 at 09:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
64-bit integers in C nodger Programming 1 03-31-2005 08:06 AM
converting integers to strings 1337 Twinkie Programming 12 08-06-2004 04:57 PM
Reading integers in a GTK textbox... Claus Programming 2 11-04-2003 08:44 PM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 09:15 AM
convert integers to ascii raven Programming 3 02-15-2002 03:32 AM

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

All times are GMT -5. The time now is 02:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration