String conversion

This is a discussion on String conversion within the C++ Programming forums, part of the General Programming Boards category; Hi I have have a sections of my program that extracts 4 bytes from a file one by one with ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    String conversion

    Hi I have have a sections of my program that extracts 4 bytes from a file one by one with the code below. I basically want the 4 bytes as a string of hexadecimal values so the only way I know how to do this is print each one off individually with the 'hex' attribute for cout.
    However now I would prefer to have these for bytes as a 'string' of hex valjues as I would like to preform a search on these 4 bytes of hex. Can any advise me on how to transfore my 4 bytes into a string of their hex values?

    any help will be much appreciated

    Code:
    unsigned char VS1,VS2,VS3,VS4;
    
    infile.read (&VS1,sizeof (VS1)) ;
    infile.read (&VS2,sizeof (VS2)) ;
    infile.read (&VS3,sizeof (VS3)) ;
    infile.read (&VS4,sizeof (VS4)) ;
    
    file_op << std::hex << (int)VS1;					
    file_op << std::hex << (int)VS2;
    file_op << std::hex << (int)VS3;
    file_op << std::hex << (int)VS4;

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    sprintf() can do it, and std::stringstream should work too.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    hello, thanks for pointing me in the right direction. Sorry to be a pain but i just cant get my head around how this works. I have found a relatively simple looking example from the internet but i cnt see how I can use this to achieve what Im after.

    I have attempted to put something together which im not sure follows the proper logic but my attempt was to transfor each unsigned char using 'x' so that the byte transforms into its hex values then store the hex values into the buffer, hopefully ending up with the buffer containing the hex values of the four bytes.

    I not even sure if my code is even on the right lines but it seems to be giving me back an error of "unterminated string or character constant in function"



    Code:
    
    unsigned char VS1,VS2,VS3,VS4;    
    
    infile.read (&VS1,sizeof (VS1)) ;
    infile.read (&VS2,sizeof (VS2)) ;
    infile.read (&VS3,sizeof (VS3)) ;
    infile.read (&VS4,sizeof (VS4)) ;
    
    
    
      char buffer [50];
      
      VS1=sprintf (buffer, "%x);
      VS2=sprintf (buffer, "%x);
     VS3=sprintf (buffer, "%x);
     VS4=sprintf (buffer, "%x); 
      return 0;

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With a stringstream and a string this might look like this:
    Code:
        char a = '[', b = 'a', c = 'z', d = ']';
        std::stringstream ss;
        
        //insert numbers into stringstream using hex formatting
        ss << std::hex << (unsigned)a << (unsigned)b << (unsigned)c << (unsigned)d;
        
        //extract string
        std::string hexstring = ss.str();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

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