Results 1 to 7 of 7

Thread: Concatenate two array elements

  1. #1
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Concatenate two array elements

    This is probably a dumb question but I am still learning so here goes....
    I have a requirement to concatenate two elements of an array.

    For example:
    Qt Code:
    1. array[0] = "f";
    2. array[1] = "e";
    3. newarray[0] = "0x"+array[0]+array[1]
    To copy to clipboard, switch view to plain text mode 
    so the result would be "0xfe"

    I have tried
    Qt Code:
    1. char result[10];
    2. char buffer[10];
    3. sprintf( result[0] , "0x%c%c", (unsigned int) buffer[0], (unsigned int) buffer[1]);
    To copy to clipboard, switch view to plain text mode 
    which compiled but gave a segmentation error when run.

    Any thoughts or suggestions would be greatly appreciated.

    Thanks, B1.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    If you are sure that newarray[0] is long enough, you can try:
    Qt Code:
    1. strcat( newarray[0], array[0] );
    2. strcat( newarray[0], array[1] );
    To copy to clipboard, switch view to plain text mode 

    Do you really need to operate on char *? QStrings are much safer.

  3. The following user says thank you to jacek for this useful post:

    b1 (10th October 2007)

  4. #3
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    Thank you jacek. That solved my problem.

    For the benefit of others here is my code:

    Qt Code:
    1. char temp[10];
    2. uint temp2;
    3.  
    4. strncat( temp, &array[0], 1);
    5. strncat( temp, &array[1], 1);
    6. sscanf( temp, "%x", &temp2);
    7. newarray[0] = temp2;
    To copy to clipboard, switch view to plain text mode 

    Thanks again,

    B1.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    Remember to initialize temp.

    Also if array is a zero-terminated string (according to your first post, it was supposed to be an array of char *), you don't need strncat() --- you can pass it directly to sscanf().

  6. #5
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    Jacek,

    I don't quite follow your suggestion
    you don't need strncat() --- you can pass it directly to sscanf().
    Wouldn't that pas the entire array to sscanf? I only want certain bits of it.

    B1.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    First of all, what is the exact type of "array"?

    If "array" is an array of strings you can do this:
    Qt Code:
    1. sscanf( array[0], "%x", &temp1 );
    2. sscanf( array[1], "%x", &temp2 );
    3. result = ( temp1 << 4*strlen(array[0]) ) + temp2;
    To copy to clipboard, switch view to plain text mode 
    this way you don't have to care about any temporary strings.

  8. The following user says thank you to jacek for this useful post:

    b1 (29th October 2007)

  9. #7
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Concatenate two array elements

    Aaaahhhh,

    The penny is starting to drop. I will have a play and see how I go. Thank you for the explanation, Jacek.

    Regards, B1.

Similar Threads

  1. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.