Hi there,

I have an array of unsigned chars SOURCE, i need to combine into DESTINATION, i want to turn:

SOURCE[0] = 3
SOURCE[1] = 9
SOURCE[2] = ' '
SOURCE[3] = 0
SOURCE[4] = 4
SOURCE[5] = ' '
SOURCE[6] = 7
SOURCE[7] = d

into

DESTINATION[0] = 39
DESTINATION[1] = 04
DESTINATION[2] = 7d

Any ideas

Many Thanks

You could do it by using some strings, streamstrings and vectors...

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define SIZE 8

int main(){
    unsigned char SOURCE[SIZE]={'3', '9', ' ','0','4',' ','7','d'};
    vector<string> destination;
    string part;
    stringstream token;
    token<<SOURCE;
    while(getline(token, part, ' ')) destination.push_back(part);
    for (size_t i=0;i<destination.size();i++)
        cout<<destination[i]<<endl;
    return 0;
}

Somehow I get the impression that he wants numeric values rather than strings. In that case it would be necessary to either convert the string to a number or build the number directly. For example:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int digit_value(char ch)
{
    const std::string digits = "0123456789abcdef";
    std::string::const_iterator it = std::find(digits.begin(), digits.end(), ch);

    if (it == digits.end())
        return -1;

    return it - digits.begin();
}

int main()
{
    const char *src = "39 04 7d";

    while (*src) {
        if (isspace(*src)) {
            ++src;
            continue;
        }

        unsigned char value = 0;

        value = digit_value(*src++);
        value = 16 * value + digit_value(*src++);

        std::cout<< (int)value <<'\n';
    }
}
#include <sstream>
#include <iostream>

int main()
{
    const char *src = "39 04 7d";
    int value;
    std::stringstream ss;

    ss << std::hex << src;

    while (ss >> value)
        std::cout << value << std::endl;

    return 0;
}

-edit-

Added a return 0. (Blame deceptikon's code for lacking that, got lazy and took that as base)
Also works with upper case. (e.g. "FF" vs "ff")

Thanks for your prompt replys.

Unfortunalty i do no get to specify what "src" is. All i have is unsigned char SOURCE[8] which i need to fit into DESTINATION[3].

The reason being a function takes unsigned char DESTINATION[3].

deceptikon: thanks for that, but i'm unable to match my SOURCE[8] to the const char *src you have used.

Thanks

Then just change his code like this: (notice it should be 9 instead of 8)
char SOURCE[9] = "39 04 7d";

Even if that's not possible, you could change it to something like this:

#include <sstream>
#include <iostream>

const int SOURCE_SIZE      = 8;
const int DESTINATION_SIZE = (SOURCE_SIZE + 1) / 3;

int main()
{
    std::stringstream ss;
    int value;

    unsigned char SOURCE     [SOURCE_SIZE]      = {'3', '9', ' ', '0', '4', ' ', '7', 'D'};
    unsigned char DESTINATION[DESTINATION_SIZE] = { 0 };

    ss << std::hex;
    ss.write((const char*)SOURCE, SOURCE_SIZE);

    for (int i = 0; ss >> value; i++)
        DESTINATION[i] = value;

    return 0;
}

-edit-
Modified it so the destination size is based on the source size.. Just set source to 8 for your example, destination will be 3 then.

I don't understand, applogies i'm fairly new to c++.

i dont get to specify

const char *src = "39 04 7d";

As the data is in an unsigned char array of [8] which i get passed. Changing deceptikon's code to the above get me into a whole of pain with the *src pointer and i already have a SOURCE[8] anyway.

Thanks

Thanks Gonbe,

nearly, couldn't get it to work with letters, the 'D' seems to get missed out

Thanks

Alex

nearly, couldn't get it to work with letters, the 'D' seems to get missed out

What do you mean?

When I add something like the following after my code:

for (int i = 0; i < DESTINATION_SIZE; i++)
    std::cout << (int)DESTINATION[i] << std::endl;

You'd get the following output:

57
4
125

Which is what I'd expect.

What is the type of DESTINATION?

What is the type of DESTINATION?

The reason being a function takes unsigned char DESTINATION[3].

deceptikon: thanks for that, but i'm unable to match my SOURCE[8] to the const char *src you have used.

It was an example only, I'm not doing your work for you. You'll obviously need to adjust the algorithm to suit your source and destination arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.