Solved

Strings HELP (NOT MFC)

Posted on 2003-11-26
5
237 Views
Last Modified: 2010-04-02
Hi People,

I have two strings and need to create one string with the data from string1 and string2 separated by a null terminator. I'm developing eVC++ 3.0

I have listed what I’m trying to achieve below...... :->

String1 = const CHAR newipStr [] = "Kx001";
String2 = char IpNew[34];

String2 = “10.5.99.99”

String3 = String1 + NULL TERMINATOR + String2

String3 = Kx001(NT)10.5.99.99

(NT) = NULL TERMINATOR

Thanks
0
Comment
Question by:LPlate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:LPlate
ID: 9826370
sorry

String2 =10.5.99.99
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 9826412
const char* string1 = "Kx001";
const char* string2 = "10.5.99.99";
char IpNew[34];

int string1size = strlen ( string1) + 1; //including it's NT
int string2size = strlen ( string2) + 1; //including it's NT

memset ( IpNew, 0, sizeof ( IpNew));

memcpy ( IpNew, string1, string1size);

memcpy ( IpNew + string1size, string2, string2size);


0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9831008
If you need to embed null terminators into strings, you may find it easier to use std::string instead of char[]. std::string doesn't treat the '\0' character as anything special (except where the constructor taking a const char* with no length is used).

--------8<--------
#include <iostream>
#include <string>

int main()
{
        const char c_string1[] = "Kx001";
        const char c_string2[] = "10.5.99.99";
        const std::string string1(c_string1,sizeof(c_string1)); // Include the '\0'
        const std::string string2(c_string2); // Don't include the '\0'

        std::string string3 = string1+string2;

        std::cout << "Here it is [" << string3 << "]\nRedirect output to a file to see the '\\0' character!\n";

        const char *data = string3.data();
        std::string::size_type size = string3.size();
        std::cout << std::hex;
        while (size--)
                std::cout << "Character is 0x" << static_cast<int>(*data++) << '\n';
}
--------8<--------
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286134
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9826412}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question