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?

Dani AI

Generated

Two quick root causes to clear up (they appear in the thread): using tiny fixed buffers with strcat causes the access violation, and casting pointers to integers and applying & to those pointers is wrong and undefined. The problem isn’t that “hex” values are special objects — they’re just integers displayed in base‑16. Convert the string to an integer, then do a normal bitwise AND; format the result as hex when you print it.

Safe, compact approach (illustrative):

#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>

/* convert hex text (like "1006" or "FF") to an unsigned integer */
uint32_t v1 = (uint32_t)strtoul(input_str, NULL, 16);
uint32_t v2 = (uint32_t)strtoul(mask_str,  NULL, 16);
uint32_t result = v1 & v2;

/* print as hex */
printf("0x%" PRIX32 "\n", result);

Notes: use strtoul/strtoull for unsigned values, choose a fixed-width unsigned type (uint32_t/uint64_t) for predictable bitwise behavior, and check the endptr/errno return values to detect malformed input or overflow.

If you must build a prefixed string like "0x1006", use snprintf with a properly sized buffer (length >= 3 + strlen(input)), or avoid adding "0x" altogether and call strtoul(..., 16). Also be careful with masks — the mask width matters. For example, 0x1006 & 0xF0 yields 0x0000, while 0x1006 & 0xFFF0 yields 0x1000. Finally, thanks to for pointing to string→number conversion and to for the test cases; correcting the buffer sizes and using numeric conversion fixes the crashes and gives the expected bitwise results.

Recommended Answers

All 8 Replies

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.