LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-25-2004, 08:07 PM   #1
tekmorph
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Mandrake 10.0
Posts: 68

Rep: Reputation: 15
gnu c++ compiler and ascii


Sorry to be posting up the forums tonight, or day depending on where you are from.
I have read and experienced that the gnu compiler does not support hexadecimal output? I believe that is right, forgive me if I am wrong. Is there a way around this? Thank you.
 
Old 10-25-2004, 08:34 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
What have you been smoking? The GNU compiler is perfectly capable of displaying hexadecimal output. Any compiler that wants to even pretend to be ANSI compliant can display hexadecimal output...

Now, the question is... How do you want to display hexadecimal output exactly, and what language are you using?

If you just want to print a hex value to the screen, you can do something like so with C:

printf("%x", someValue);

Or the following with C++:

cout << hex << someValue; // May want to cast someValue to an int first...
 
Old 10-25-2004, 08:47 PM   #3
tekmorph
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Mandrake 10.0
Posts: 68

Original Poster
Rep: Reputation: 15
Sorry , thats what I have read twice today. I get the error :segmentation fault when I try.
I am only 3 or 4 months into this so cut me some slack, but thanks. I was reading some of my posts from before and I was chuckling myself, guess I will add a new one to laugh at sometime. I went to the site I got the info from and this is why I was confused.

hex
Print the hexadecimal representation numbers as they are stored in memory. For example, on a workstation which stores 8 byte real values in IEEE format with the least significant byte first, the value of pi when printed in hex format is 400921fb54442d18. This format only works for numeric values.

I am wanting to convert a string of characters in c++ . They are non numeric. For instance a name. Thanks again.


Last edited by tekmorph; 10-25-2004 at 09:11 PM.
 
Old 10-25-2004, 09:22 PM   #4
khtse
Member
 
Registered: Sep 2003
Location: Hong Kong
Distribution: Slackware 9.1
Posts: 43

Rep: Reputation: 15
i think you need to be a bit more specific about your problem and which part of your code is causing the seg-fault.
As deiussum said, the GNU Compiler IS capable of displaying hex....
 
Old 10-25-2004, 09:33 PM   #5
tekmorph
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Mandrake 10.0
Posts: 68

Original Poster
Rep: Reputation: 15
I believe you when you say it will display hex.
I misread that part of the tutorial. However it does state that it only works on numeric values. To error is human.
This code echos the string input. Is there a way for it to display input as a hex value. Again thank you.
{
string input;
cout << Enter your name:
cin >> input;
cout << hex << input;
return 0;
}
 
Old 10-26-2004, 08:13 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
For strings, you'll probably need to print each character one at a time. Generally, when I print hex characters, I also like to print a 0 in front of hex values less than 10, so like 0F instead of F... The following code should do that for you

Code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string input="test";

    for(int c=0;c<input.length();c++)
    {
        cout << hex << setfill('0') << setw(2) <<
           static_cast<int>(input[c]) << " ";
    }

    return 0;
}
P.S. Sorry if I came across too strong in my previous post. I didn't mean it to come across as insulting in any way.

Also, segmentation faults usually occur when you are touching memory in some way that you have no right to be touching it. (Shame on you! That's a bad touch!) That includes things like dereferencing dangling pointers, going beyond the bounds of an array, trying to change memory that is flagged as "constant," etc.

Last edited by deiussum; 10-26-2004 at 08:18 AM.
 
Old 10-26-2004, 09:13 AM   #7
tekmorph
Member
 
Registered: Aug 2004
Location: Ohio
Distribution: Mandrake 10.0
Posts: 68

Original Poster
Rep: Reputation: 15
very cool, no I didn't feel insulted. I am new so I will probably ask questions, or as in my previous post , make statements that others would find entertaiting. Again thank you.
 
  


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
Help with GNU C++ Compiler please... Stiltz Linux - Software 1 03-15-2005 09:17 PM
Gnu Compiler On Fedora 2/3 borisc General 1 02-04-2005 06:49 PM
where is GNU gcc compiler? karfei00 Linux - Newbie 5 06-16-2004 12:55 PM
help with installing Gnu C compiler little_arab Mandriva 1 04-01-2004 12:45 PM
Where to get compiler gnu c sofasurfer Linux - Games 1 01-11-2004 01:25 AM

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

All times are GMT -5. The time now is 12:12 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