Thread: Basic Question - How to convert char array element to a string?

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Basic Question - How to convert char array element to a string?

    So I want to create a string which consists of multiple pre-defined chars, but I'm not sure how to do this.

    I want to say something like

    Code:
    char Txdata[] = Buffer[0][j], Buffer[0][i];
    (Where Buffer is an array of chars) But I know that's definitely not right.

    Is it a simple syntax job, or do I need to do something like type casting?

    Thanks for any help!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Try
    Code:
    char Txdata[3];
    Txdata[0] = Buffer[0][j];
    Txdata[1] = Buffer[0][i];
    Txdata[2] = '\0';

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by algorism View Post
    Try
    Code:
    char Txdata[3];
    Txdata[0] = Buffer[0][j];
    Txdata[1] = Buffer[0][i];
    Txdata[2] = '\0';
    or even:

    Code:
    char Txdata[] = { Buffer[0][j], Buffer[0][i], 0 };
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Elkvis View Post
    or even:
    Code:
    char Txdata[] = { Buffer[0][j], Buffer[0][i], 0 };
    That's more like it!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AlecMK
    Where Buffer is an array of chars
    Are you sure? If Buffer is an array of chars, then Buffer[0] is a char, so Buffer[0][j] is an error, assuming j is an index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    Quote Originally Posted by Elkvis View Post
    or even:

    Code:
    char Txdata[] = { Buffer[0][j], Buffer[0][i], 0 };
    Thanks for the reply. However, I get an error with this "constant expression required"

    Quote Originally Posted by laserlight View Post
    Are you sure? If Buffer is an array of chars, then Buffer[0] is a char, so Buffer[0][j] is an error, assuming j is an index.
    Buffer is a 2D array of chars, as declared:

    Code:
    char Buffer[100][5];


    I realise that I don't think I was quite clear what I'm after. So I'm looking to transmit the Txdata as a string, so for example I could write

    Code:
    char Txdata[] = "Test";
    Which would transmit Test.So say for example Buffer[0][1] = 5, and Buffer [0][2] = 10, then the end result I'd hope for would be the equivalent of sending:

    Code:
    char Txdata[] = "5, 10, ";
    Any thoughts? I'm not sure at all on this!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AlecMK
    However, I get an error with this "constant expression required"
    Elkvis' suggestion is only applicable if you are compiling with respect to C99 or later. You can still use algorism's suggestion.

    Quote Originally Posted by AlecMK
    So say for example Buffer[0][1] = 5, and Buffer [0][2] = 10, then the end result I'd hope for would be the equivalent of sending:
    Code:
    char Txdata[] = "5, 10, ";
    Any thoughts?
    This means that you want to convert and combine the byte value of Buffer[0]'s elements to a numeric string format. You can do so with sprintf, e.g.,
    Code:
    char Txdata[11];
    sprintf(Txdata, "%d, %d, ", (int)Buffer[0][1], (int)Buffer[0][2]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert a string into a 2d char array?
    By UltimoBrah in forum C Programming
    Replies: 6
    Last Post: 03-08-2013, 08:16 AM
  2. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  3. convert element of char* to a character
    By nicoeschpiko in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 11:39 PM
  4. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  5. convert string to char array
    By Dan17 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2006, 11:47 PM