Thread: String conversion problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    hello, i also have some conversion problems.

    input:

    char InBuff[8] = "12345678";


    output:

    char OutBuff[6] should be '123456' first 6 digits
    char OutB should be '78' last 2 digits (bcd encoded)


    the implementation should be a one liner.this is what a have so far.it works, aslong no 0 is involved

    OutB = ((InBuff[6] - 0x30) * 10) + (InBuff[7] - 0x30);

    can anyone help me out here and show me a good implementation or give me some hints plz.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like it should work, though it would be better to write:
    Code:
    OutB = ((InBuff[6] - '0') * 10) + (InBuff[7] - '0');
    How does it not work?
    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

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    thx, i thought there is no difference between them.i need to read this again.

    ok it does not work if we have a zero at the first position of the last 2 bytes.

    lets say we have this:

    incoming buffer "12345608"

    now the outgoing char value should be "08", but this impementation returns "8". thats the problem i need to solve.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    now the outgoing char value should be "08", but this impementation returns "8". thats the problem i need to solve.
    Oh. This is just a formatting problem. You may need to write:
    Code:
    printf("%02d\n", OutB);
    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

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    char InBuff[8] = "12345678";
    "12345678" is a string literal. It's actually 9 characters long "12345678" <- your string + '\0' null terminator. So basically you have an array bounds overflow. It should be:
    Code:
    char InBuff[9] = "12345678";
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    hmm, does that mean i can use some printf family functionallity to store the data in a char. ok i will try it and post the result later on.

    thx Brad, you are absolutely right. but i do not use a literal, the buffer array is dynamically allocated, and then filled with (fixed length) data without null termination.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    does that mean i can use some printf family functionallity to store the data in a char
    No. The value stored in the char is 8. Whether you print it as 0 or as 08 is another matter. If you want to store "08", then you cannot use a char, but an array of chars.
    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

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    ok, thanks laserlight. i see this isnt as trivial as i thought. looks like i need to get rid of the oneline idea, cause i do not have any other option then 1 single char.

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by Mr.Bit View Post
    hello, i also have some conversion problems.

    input:

    char InBuff[8] = "12345678";


    output:

    char OutBuff[6] should be '123456' first 6 digits
    char OutB should be '78' last 2 digits (bcd encoded)


    the implementation should be a one liner.this is what a have so far.it works, aslong no 0 is involved
    You want to rip the last two characters of the string InBuff in a variable named OutB in a
    one-liner statement? That is not so hard actually, just strcpy, memcpy whatever starting at 2 places befor the end of InBuff.
    The end of InBuff can be found by adding to the address where InBuff starts it's string length. That is the character that terminates InBuff, which contains a '\0'. Now just remove 2, and there is your place.
    Then strcpy, or memcpy from that location whatever is your choice. Cause as you said, if it is say '08' the conversion to a number will kill your 0, but you want it am i correct?
    If that is the case it can be easily done in one line, by just performing all the calculation for the appropriate offset at the parameter of the copying function.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    yes, you are correct.i need the exact same value in this char, even if its '00'.

    im not sure if i fully understand, however i will try it and post the result later on. i need to go home now, its about 11 oclock and im still at the office!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Problem with string output...
    By Lau in forum C Programming
    Replies: 6
    Last Post: 11-20-2003, 10:51 AM
  4. string object to null-terminating string conversion
    By sayz0 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 12:15 PM