CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Posts
    10

    Save HEx output to Variable

    I've been trying to teach myself C++ over the years and here is another shot at it.

    I know how to convert a charcter to its ASCII equivilent(sp?) and then to it's hex equivilent and print the hex number out.

    Example:
    Code:
    cout << hex << int(line[a]);
    But I want to save the hex output to a variable. I'm not sure whether it should be a string or an int, or even how you go about coding it.

    My Guesses:
    out_line[a] = hex(int(line[a]));
    out_line[a] = hex << int(line[a]);

    I do have a background in Visual Basic (megar background, started at age 7, am now 18), and could do this real easy in VB but I want to learn another language so I'd figured I'd give it a shot.

  2. #2
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Save HEx output to Variable

    Hi,

    unless and until you store value as a string , all values are stored in the form of binary.

    int i = 0x05; //hex
    i=5; //decimal
    i=05;//octal

    value of this no is 5 i.e 0101.

    so value is the same only representation and symbol changes as per the no. system. while displaying you need to take care of the no system and its representation.

    -Anant
    "Devise the simplest possible solution that solves the problems"

  3. #3
    Join Date
    Dec 2007
    Posts
    10

    Re: Save HEx output to Variable

    I'm sorry but I'm completely new at this. I've been trying to find books to get a little reading done on C++ but haven't had much luck.

    You are basically saying that cout << hex << int(line[a]) is returning an integer value, and I need to convert it to a char/string value in order to use it?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Save HEx output to Variable

    Quote Originally Posted by Neon612
    I know how to convert a charcter to its ASCII equivilent(sp?)
    There is nothing to convert.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       char c = 'A';
       if ( c == 65 )
            cout << "This is equal to 65\n";
      if ( c == 0x41 )
            cout << "This is equal to 0x41\n";
    }
    You will see that both cout statements are executed, since an 'A' is equal to 65 or 0x41. Characters are already numbers -- the representation when you output the character is what is different.

    If you want to store the output into a string, use stringstreams or the unsafe sprintf() function.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured