Home Help Center

Casting char* to std::string

inmmatinmmat Member
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.

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    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.
  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    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
  • inmmatinmmat Member
    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?.
  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    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:
    // 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.
  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    And you can cast your sequence of bytes to char* using reinterpret_cast
    MyByteSeq bytes;
    reinterpret_cast<char*>(&bytes[0])
    
Sign In or Register to comment.