Hi,

Can anyone advise on how I can convert a string to a hex string? (And subsequently perform an AND operation on two hex strings.) I have a string value passed in (e.g., 1006), and I want to turn that into hex (0x1006).
I have tried the following:

[B]// Assume [I]inputData[/I] and [I]mask[/I] to be strings (e.g., "1006", "FF")[/B]
    unsigned char *hexResult;
    char *hex1[20];
    char *hex2[20];
    *hex1 = "0x";
    *hex2 = "0x";

    strcat(*hex1, inputData);
    strcat(*hex2, mask);

    hexResult = (unsigned char *)((int)hex1 & (int)hex2);

At the first strcat operation, an access violation is occurring.


I have also tried another way:

[B]// Assume [I]inputData[/I] and [I]mask[/I] to be strings (e.g., "1006", "FF")[/B]
    unsigned char *hexResult;
    char hex1[3] = "0x";
    char hex2[3] = "0x";

    strcat(hex1, inputData);
    strcat(hex2, mask);

    hexResult = (unsigned char *)((int)hex1 & (int)hex2);

But during debugging, when the second strcat operation is performed, the value in hex1 appears to be getting lost, and is set to "". Any ideas why this happens? Or suggestions as to how I can resolve?

I'm not sure what you mean by a 'hex string'...Do you meant something like

char hex_string[] = "0xffddee99";

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ARR_SIZE 6

int main(int argc, char**argv)
{
	char hex1[10] = "0x";
	char hex2[10] = "0x";

	char ch[ARR_SIZE];

	fputs("enter some hex->", stdout);
	fgets(ch, ARR_SIZE, stdin);

	strcat(hex1, ch);

	fprintf(stdout, "You entered->%s\n", hex1);

	/*
		now your working with a string of characters so you'll
		have to convert your string into a integer with strtol()
	*/

	exit(EXIT_SUCCESS);
}

Yes, I will read in a string something like "1006", and I want to change that to "0x1006", which I can then cast to an int to get the correct value (and perform an AND operation on that and another hex string; e.g., (0x1006 & 0xFF)). Does that make sense?

Yes, I will read in a string something like "1006", and I want to change that to "0x1006", which I can then cast to an int to get the correct value (and perform an AND operation on that and another hex string; e.g., (0x1006 & 0xFF)). Does that make sense?

Yeah it does now...You'll have to convert(not cast) your string into an integer with strtol.

Ok, I now have the following:

char *endp;
	long value1, value2;

        unsigned char *hexResult;

	char hex1[3] = "0x";
        char hex2[3] = "0x";

	strcat(hex1, inputData);
	value1 = strtol(hex1, &endp, 16);

	strcat(hex2, mask);
	value2 = strtol(hex2, &endp, 16);

Now value1 and value2 hold the decimal values for the two hex inputs. What I need to do is perform an AND operation on the two hex values when I get them into hex. E.g.,

0x1006 & 0xF0 = 0x1000

I know that hex values can be represented as integers. i.e. I can do something like:
int memAddr = 0x1fe;

And that I can perform an AND operation on two hex (int) values. How can this be achieved from the above?

Ok, I now have the following:

char *endp;
	long value1, value2;

        unsigned char *hexResult;

	char hex1[3] = "0x";
        char hex2[3] = "0x";

	strcat(hex1, inputData);
	value1 = strtol(hex1, &endp, 16);

	strcat(hex2, mask);
	value2 = strtol(hex2, &endp, 16);

Now value1 and value2 hold the decimal values for the two hex inputs. What I need to do is perform an AND operation on the two hex values when I get them into hex. E.g.,

0x1006 & 0xF0 = 0x1000

I know that hex values can be represented as integers. i.e. I can do something like:
int memAddr = 0x1fe;

And that I can perform an AND operation on two hex (int) values. How can this be achieved from the above?

Right away your character arrays are too small. You have

char hex1[3] = "0x";

and then you

strcat(hex1, inputData);

hex1 is only three characters long...i.e It already is filled with '0', 'x', '\0' so you can't append anything onto it without overflowing it.

Oh that works better. Thank you. I'll try to work on ANDing them now.

I now have the two hex strings as follows:

hex1_string = "0x1006"
    hex2_string = "0xFF"

Is there an easy way to change these from strings, so that they can be represented as integers (retaining their hex value)? I need the result of the conversion to be something like:

int hex1_int = 0x1006;
    int hex2_int = 0xFF;

Every conversion I try gives me their decimal value.

Yes use strtol()..

The value is the same, hex or integer, its just a matter of how you choose to display it.

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.