0

I've tried memcpy / strncpy / std::copy but neither of them seem to work resulting the program to crash.

Here's what i need to do:

I'm trying to parse arguements from user input for instance. "message -add 0xAE"

i need to fetch the 0xAE part into an integer, heres some pseudo-code _input will hold the full string "message -add 0xAE"

if(strstr(_input,"message -add 0x")){
    char* _temp;
    std::copy(strlen("message -add 0x"),strlen("message -add 0x")+(strlen(_input)-strlen("message -add  0x")),_temp);
    /* or */
    memcpy(_temp,_input+strlen("message -add 0x"),strlen(_input)-strlen("message -add 0x"));
    int _value = (int)_temp;
    CLog->out("\n\n %d",_value);
}

Edit: Thanks Alan!

if(strstr(_input,"message -add 0x")){
            char* _temp = new char[strlen(_input)-strlen("message -add 0x")];
            memcpy(_temp,_input+strlen("message -add 0x"),strlen(_input)-strlen("message -add 0x"));
            int _value = atoi(_temp);
            CLog->out("\n\n %d",_value);
}
7
  • I can't see the part of code where you attempt to convert a string to an integer Commented Oct 8, 2011 at 19:05
  • 2
    _temp isn't initialised. Commented Oct 8, 2011 at 19:08
  • Whilst you don't want to use any 3rd party code and therefore are ruling out all good options parsers, your attempts to do this yourself are liable to end with many many defects. Why would you wish to reinvent this particular wheel? Parsing options is surprisingly hard. Unless you are a highly skilled developer you should be happy to use 3rd party code that works. Commented Oct 8, 2011 at 19:16
  • 1
    @David Hefferman Sorry if I sounded a bit offensive, i didn't mean any offense. I'd like to thank you for your time. Others might say you're wasting it, but passing knowledge is far better than mindlessly watching football all night.At least in my honest opinion.You have answered all of my questions so far so I'd like you to know how much I appreciate it! Keep up the good work! Commented Oct 8, 2011 at 19:30
  • 1
    No offense taken at all. I'm just not sure what level you are at and what your goals are. Keep it up yourself!! Commented Oct 8, 2011 at 19:34

2 Answers 2

1

Are you looking for:

int value = 0;
char c;
for(int i = strlen("message -add 0x"); c = *(_input + i); i++) {
    value <<= 4;
    if(c > '0' && c <= '9') {
        // A digit
        value += c - '0';
    } else if(c >= 'A' && c < 'G') {
        // Hexadecimal
        value += 10 + c - 'A';
    }
}

?

Sign up to request clarification or add additional context in comments.

8 Comments

It doesn't work properly. If i enter message -add 0x1 .. it returns 203 or something.
@minitech looks like you forgot to initialise value to 0
Won't work; value isn't initialized. You're also treating the input as decimal when it's hex.
@CareyGregory: I'm too used to VB.NET right now, sorry. And I was thinking the alphabet came right after the numbers. I do read questions properly, even if I don't think straight all the time :)
@minitech: My condolences on the vb.net thing ;-)
|
1

If you want to use C++, then steer clear of the various C functions and all of that nasty memory management. I would recommend reading Accelerated C++. It really is a top-notch book for learning C++ and actually using C++. Here's another solution to your problem without the C string parsing routines:

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>


int
main()
{
    std::string const TARGET("message -add 0x");
    char const _input[] = "message -add 0xAE23";

    std::string input(_input);
    std::string::size_type offset = input.find(TARGET);
    if (offset != std::string::npos) {
        std::istringstream iss(input);
        iss.seekg(offset + TARGET.length());
        unsigned long value;
        iss >> std::hex >> value;
        std::cout << "VALUE<" << value << ">" << std::endl;
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.