0

hello all, this is my first time posting. Okay, I'm reading a character array from a .dat file. The character array has 6 elements and I'm supposed to fold the integer values of the character array. The professor wants us to pair up consecutive indexes and add them together. For ex: "ABCDEF" would be 6566 + 6768 + 6970. This is part of an assignment to create a hash table.
Could someone please tell me how to fold the character array in this manner?
Thanks.

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by ssv
0

May be this code sample will be of some help.Just have a look at it.

/***** required macros and structures *****************/
union example
{
	char array[6];
	struct ints
	{
		short a;
		short b;
		short c;
	}x;
};

union example e1;
#define swapbytes(z) (((short)(z << 8)) | ((short)(z >> 8)))


/********************example code snippet ****************/
int main(int argc, char* argv[])
{

	memcpy(e1.array,"aBCDEF",6);
	printf ("value of array = %s\n",e1.array);
	printf("value of a = %X\n",swapbytes(e1.x.a));
	printf("value of b = %X\n",swapbytes(e1.x.b));
	printf("value of c = %X\n",swapbytes(e1.x.c));
	printf("value of a + b  + c = %X\n",swapbytes(e1.x.a) + swapbytes(e1.x.b) + swapbytes(e1.x.c));
	printf("Hello World!\n");
	return 0;
}

Code tags added. -Narue

bye
ssv

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.