Return to Website

C++ / VC++ / MFC Discussion Forum

Welcome to the forum. Now, you can use HTML in your posts too.

C++ / VC++ / MFC Discussion Forum
Start a New Topic 
Author
Comment
data types understanding....

well, thank you very much about your article on expert-exchange.com ,, well, what i am asking about , is how can i put four chars in a ulong,, i do know that ulong=4bytes, and char = 1 byte, i do like that:

ulong value1 ;
char ss[]="name";

value =ss ;

that is the problem ,, i don't want to put the first char in the ulong,, i do want to put the whole string in it,,, i will be thankful if you reply me ,

Re: data types understanding....

Here's the code to convert a string to a ULONG and
vice versa. But please, take a careful look at the
note at the end of the code.

ULONG ulValue;
char szTestString[]="nam";
ulValue =(ULONG)szTestString; // Now ulValue contains "nam"

char szCheckString [ 4 ] ;
memcpy(szCheckString,(void * )ulValue,4); // Now, szCheckString contains "nam"

NOTE : I have used the test string as "nam" and NOT
"name" bcoz char is one byte agreed and ULONG is 4
bytes , agreed. But what about the NULL termination
character. If u use the string "name", its actually
4 characters PLUS one character for NULL. That makes
it 5 CHARACTERS. That cant be possibly put into a
ULONG. It will only lead in buffer overflows and
unpredictable results.

Hope this helps. Have a nice day.

Re: data types understanding....

what about this code?
union a
{
struct b{
char c ;
};
ulong d;
};
union a x;
x.b.c ='n';
x.b.c = 'a';
x.b.c ='m';
x.b.c ='e';
char name[]="name"
for(int i=0; i < strlen(name);i++)
x.b.c = name ;
ulong u = x.d;