String copying

This is a discussion on String copying within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a string of integers which need converting from ASCII to BCD, which I do so with the ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    String copying

    Hello,

    I have a string of integers which need converting from ASCII to BCD, which I do so with the following method:
    Code:
    static void ascii2bcd(char* outbuf, int length, char* data)
    {
    	int i;
    	for (i = 0; i < length; ++i)
    	{
    		char val = *data++;
    		*outbuf++ = val & 0xF;
    	}
    }
    This appears to work fine. However, I would like to copy the contents of outbuf into a char[], which is where the problem appears to arise.

    I can call the method and it will successfully enter the data into the outbuf, like so:
    Code:
    	char* data = (char*)malloc(17);
    	bcd2ascii(data, 17, (char *)"35496200179252501");
    I then move the contents of the char* outbuf into a char[] like so:
    Code:
    	char result[64];
    	int i;
    	for (i = 0; i < 17; ++i)
    	{
    		char val = *xdts5data++;
    		test[i] = val;
    	}
    I would now like to move the contents of result into another char[64], but I can't use strcopy because it truncates the data after the seventh character. I think this is because it is technically not a character anymore, so stops the copying.

    Unfortunately my C is extremely limited (as you can tell!).

    Could someone please explain how I can get result[64] into another char[64] of my choice?

    Many thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a NUL-character ('\0') in your data, then strcpy() will stop at that point.

    You can use memcpy() to assign a chunk of memory to any other equal (or larger) size chunk of memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You cannot fit "35496200179252501" into a char[17] (which is what your data is, essentially). So your claim that it will successfully enter the data is not actually correct. Once you have allocated eighteen or more characters available in data, that will be correct.

    And strcpy will only copy 18 characters because there are only eighteen characters in your string. Why would it keep going?

    Edit: After reading mats' response, trying to do strcpy on the BCD string won't work, because 0's will become \0 as he said. My response above is based on copying the ASCII version.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    To be const-correct, change:
    Code:
    static void ascii2bcd(char* outbuf, int length, char* data)
    to:
    Code:
    static void ascii2bcd(char* outbuf, int length, const char* data)
    You can then write:
    Code:
    bcd2ascii(data, 17, "35496200179252501");
    I then move the contents of the char* outbuf into a char[] like so:
    It looks like you edited the code snippet but forgot to change the variable names consistently, so now you have result, xdts5data and test.

    I would now like to move the contents of result into another char[64], but I can't use strcopy because it truncates the data after the seventh character. I think this is because it is technically not a character anymore, so stops the copying.
    If your char array has embedded null characters, you can still copy with a loop, or simply use std::copy() from <algorithm>. In fact, you have already written such a loop, methinks, to copy from the char* to the char[64].

    EDIT:
    Oh, yes, or memcpy(). By the way, have you considered using std::vector<char> or std::string?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, good points.

    But strcpy() doesn't work on a "string" where you have taken ascii numbers and done "ch & 0xF" on each character - at least not if there is a '0' (character of zero, value 0x30) somewhere in the "string", as strcpy() will see that as a end of string character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    You cannot fit "35496200179252501" into a char[17] (which is what your data is, essentially).
    You can if you ignore the terminating null character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by laserlight View Post
    You can if you ignore the terminating null character.
    Yes -- I saw "bcd2ascii" and thought that that actually was bcd to ascii, rather than the ascii to bcd function from above. So, yeah.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    All,

    Thank you for your comments. Yes, my code snippet wasn't very consistent!

    memcpy worked admirably. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21