Re: Little help on stream...

From: Mathieu Malaterre (malatNOSP_at_Mfree.fr)
Date: 10/08/04


Date: Fri, 08 Oct 2004 05:02:40 GMT

Victor Bazarov wrote:
> "Mathieu Malaterre" <malatNOSP@Mfree.fr> wrote...
>
>>I am trying to get rid a of sprintf in a c++ code, but I tried in several
>>ways and couldn't figure out how to change:
>>
>>uint16_t group, uint16_t element;
>>sprintf(buffer, "%04x|%04x", group , element);
>>
>>so far I have something like:
>>
>>std::ostringstream buf;
>>buf.flags ( std::ios_base::right |std::ios_base::hex );
>>buf.width( 4 );
>>buf << group;
>>buf << "|";
>>buf << std::hex << element;
>>std::string key = buf.str();
>>
>>But doesn't seems to work...
>
>
> #include <sstream>
> #include <string>
> #include <iomanip>
>
> int main ()
> {
> int group = 10, element = 20;
> std::ostringstream buf;
> buf << std::right << std::setw(4) << std::setfill('0') << std::hex <<
> group
> << "|"
> << std::right << std::setw(4) << std::setfill('0') << std::hex <<
> element;
> std::string key = buf.str();
>
> return 0;
> }
>
> And, just between us, you don't have to get rid of 'sprintf'. The entire
> C Standard Library is part of C++ Standard Library. If something's easier
> to do with a C function, by all means, do it.

Yes I know this is true on linux. But I have had so many weird random
bugs on Windows when I mixed old cstring and new string that now I
really try to choose one OR the other...

Thanks anyway you saved me a *lot* of time...
Mathieu
Ps: Of course I'd appreciate any comment if somebody know what is going
on. I suspect it has to do with 'cout' or 'cerr' that coud be declared
as static...



Relevant Pages

  • Re: String tokenizer for CString?
    ... I needed to be able to split a string that had double quotes and multiple delimiters. ... void SplitPath(BOOL UsingDirsOnly, CString Path, CString& Drive, CString& ... Splits a CString into an CStringArray according the Deliminator. ... int SplitLine; ...
    (microsoft.public.vc.mfc)
  • Re: any idea on how do i convert a string value to an byte
    ... Start with the last character/digit and subtract 48 from its ASCII value. ... // cast the string pointer to a CString to simplify life... ... int len = string.GetLength; ...
    (microsoft.public.vc.mfc.docview)
  • Re: CString hex value to CString decimal value
    ... > A CString contains a hexadecimal string value, ... > and then put back as a string into an other CString. ... > that converting a large hex string to int does not work. ...
    (microsoft.public.vc.mfc)
  • CString hex value to CString decimal value
    ... A CString contains a hexadecimal string value, ... I have googled and found a lot of solutions (hex string to int) ... that converting a large hex string to int does not work. ...
    (microsoft.public.vc.mfc)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)