Results 1 to 6 of 6

Thread: Casting char* to std::string

  1. #1
    inmmat is offline Registered User
    Join Date
    May 2006
    Location
    Membrilla (Ciudad Real)
    Posts
    2

    Casting char* to std::string

    Hi,

    My problem is about a casting from char* to std::string and back. I know how to do it,using c_str().

    In my program I read sound in a char* and then I have to send it to other PC with a std::string. When I receive the std::string in the second PC I have to come back to char* in order to play it.

    The problem is that when I play the sound in the second PC there is a lot of noise.

    Is there any other possibility to do this casting without losses?.

    Thanks a lot.

  2. #2
    marc's Avatar
    marc is offline ZeroC Staff
    Name: Marc Laukien
    Organization: ZeroC, Inc.
    Project: The Internet Communications Engine
    Join Date
    Feb 2003
    Location
    Florida
    Posts
    1,860
    Welcome to our forums!

    Please see this thread regarding our support policy here on these forums.

    Also, please keep in mind that this forum is really for questions specific to Ice, and not for general C++ programming.

  3. #3
    xdm's Avatar
    xdm
    xdm is offline ZeroC Staff
    Name: Jose Gutierrez de la Concha
    Organization: ZeroC, Inc.
    Project: Ice Developer
    Join Date
    Sep 2003
    Location
    La Coruņa, Spain
    Posts
    588
    hi inmmat

    is dificult to know what is your problem without view your code, cast char* to std::string is not Ice specific problem, i use it in multiple parts of my current app and i don't see any data lose.

    I think that use std::string to send no string data is not a good idea. you must use Ice::Byte instead Ice::Byte is guaranted to be the same binary representation in all supported plataforms.

    Remember to fill your signature, as marc said. If you want that zeroc people answer your questions.

    hope this help

  4. #4
    inmmat is offline Registered User
    Join Date
    May 2006
    Location
    Membrilla (Ciudad Real)
    Posts
    2
    Hi,

    Thanks for your answer. As you said me it is not a good idea to send over a std::string a no string data. I know that I have to use Ice::Byte, but my problem is that I don't know how to do the casting from char* data to Ice::Byte data.

    Could someone help me?.
    Alarcon Torres I.

    ---Student-----
    Telecommunication Engineer
    ETSIT (Madrid) Spain.
    UCLM (Ciudad Real) Spain

  5. #5
    marc's Avatar
    marc is offline ZeroC Staff
    Name: Marc Laukien
    Organization: ZeroC, Inc.
    Project: The Internet Communications Engine
    Join Date
    Feb 2003
    Location
    Florida
    Posts
    1,860
    You must use sequence<byte> in Slice, which is mapped in C++ to std::vector<Ice::Byte>. Ice::Byte is mapped to char. So all you have to do is to initialize a vector<char>, such as:
    Code:
    // Slice
    sequence<byte> MyByteSeq;
    
    // C++
    char* myData = ...; // Your data
    int len = ...; // How many bytes
    MyByteSeq myByteSeq(myData, myData + len);
    For more information, please consult suitable C++ and STL documentation.

  6. #6
    xdm's Avatar
    xdm
    xdm is offline ZeroC Staff
    Name: Jose Gutierrez de la Concha
    Organization: ZeroC, Inc.
    Project: Ice Developer
    Join Date
    Sep 2003
    Location
    La Coruņa, Spain
    Posts
    588
    And you can cast your sequence of bytes to char* using reinterpret_cast

    Code:
    MyByteSeq bytes;
    reinterpret_cast<char*>(&bytes[0])

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. session using std::wstring instead of std::string
    By Andrew in forum Help Center
    Replies: 2
    Last Post: 01-23-2008, 08:06 AM
  2. std::string in interface
    By dthompson in forum Help Center
    Replies: 1
    Last Post: 06-21-2006, 04:49 PM
  3. problem in mapping string->string[] in C#
    By teayu in forum Help Center
    Replies: 2
    Last Post: 10-26-2004, 11:12 PM
  4. I need char pointers (char *)
    By catalin in forum Help Center
    Replies: 5
    Last Post: 08-16-2004, 04:33 PM
  5. Replies: 4
    Last Post: 05-28-2004, 05:52 PM

Posting Permissions

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