Hex n' junk

This is a discussion on Hex n' junk within the C++ Programming forums, part of the General Programming Boards category; What's locale loc; for? Can't you just do ... Code: #include <iostream> #include <string> using namespace std; int main() { ...

  1. #16
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    What's

    locale loc;

    for? Can't you just do ...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string thing = "asdf";
    
    	cout<< endl << thing;
    	for ( int i=0; i<4; i++ )
    	{
    		thing[i] = toupper( (int) thing[i] );
    	}
    	cout<< endl << thing;
    
    	return 0;
    }

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Out of the two methods, only twormers worked.
    But that one only converts the first letter.

    How can I convert them all ?

    (Sorry if i'm bieng stupid here, and have missed something obvious.)

  3. #18
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Converts all the letters when I run it...

    [update]
    That locale loc seems very wrong - toupper takes only 1 argument
    [/update]
    Last edited by Richie T; 05-19-2006 at 10:39 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Just incase this makes a difference, there are some numbers in between the letters I wish to convert.

    Dunno if this matters or not.

  5. #20
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    No it shouldn't matter - you can test this in twomers program
    by replacing the "asdf" with "1234" - output is unchanged for
    numbers.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    I think you can just use std::transform() here:
    Code:
    #include <algorithm> // for std::transform
    #include <string>    // for std::string
    #include <cctype>    // for toupper
    #include <iostream>  // for std::cout and std::endl
    
    int main()
    {
    	std::string str = "asdf123";
    	std::cout << str << std::endl;
    
    	std::transform(str.begin(), str.end(), str.begin(), toupper);
    	std::cout << str << std::endl;
    }
    Last edited by laserlight; 05-19-2006 at 11:16 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by laserlight
    I think you can just use std::transform() here:
    It's been a while since I googled this one, but I seem to remember some kind of issues with that.
    http://groups.google.com/group/comp....f0e60773407338
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    It's been a while since I googled this one, but I seem to remember some kind of issues with that.
    Yep, on a hunch that there might be a problem I tested with both the MinGW port of GCC and MSVC8. std::toupper gave no compilation problems with MSVC8, but choked on GCC. The old C toupper worked in both cases.

    I did a check in Josuttis' The C++ Standard Library, and his example (chapter 11, section 2.13) was the same as my modified version, with the condition that the global locale is used.
    Last edited by laserlight; 05-19-2006 at 11:29 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    How come transform only works if your'e not using namespace std ?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    How come transform only works if your'e not using namespace std ?
    My guess would be that the using declaration brings std::toupper into scope, so you then face the same problem as I did with the MinGW port of GCC.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Oh, but is there a way to do it in lower case.
    Are you wanting the hex output in lowercase? If yes, you can just leave out the uppercase flag:
    Code:
          ss << hex << static_cast<unsigned int> (my_string[i]);
    At any rate, std::transform and std::tolower are worth learning.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Yeah, the previously mentioned method worked.
    Cheers swoopy, owe you one.

  13. #28
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Ancient Dragon
    >>would it help if I say they were all separated by a full stop
    nope -- I have no idea what a "full stop" is. They should be separated by spaces, tabe or CR/LF (Windows) or LF (*nix).
    You used 5 full stops in your post I think they're known as "periods" in the US.

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah. Sorry I'm from England.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 05:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 06:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21