Simple Network Program Difficulties

I'm trying to write 2 programs, client & server, that communicate with integers, however, all resources I have found on the net assume that you want to send and recieve information as a character array. I don't want to send my integers as characters, I want to send them as ints (casting them to characters makes them take more space). Does anyone know how to do this? Here is my working code for sending & receiving characters:

char rcvMessageChars[STRING_SIZE]; /* char message */
char sndMessageChars[STRING_SIZE];

write(newsockfd, sndMessageChars, STRING_SIZE);
msgLength = read(newsockfd, rcvMessageChars, STRING_SIZE);

Another quick question - is there any *easy* way to generate random integers in a certain range? i.e. 1-1000 instead of 1-2^16 ?

How about generating random lowercase characters a-z?

Thanks in advance for your help

No it doesn't. If your int is, say, 4 bytes long, then casting will give you an array of 4 characters. But see this post for a discussion of the macros you should use.

If "oldrand" is a random number in the range 1 to 2^16 then use something like:
mymax = 1000;
newrand = oldrand*mymax/(2^16);

But take a look at the docs for your random number generator. Is the range 2^16 or 2^15? You want to get this right.

Same problem really:
mymax = (int)'z' - (int)'a' + 1;
newrand = oldrand*mymax/(2^16);
ranchar = (char) ((int)'a' + newrand);

I wonder if you speak about two kinds of 'characters' lets say that an array contains

"ABCD" - those are four characters.
If you treat that as an integer you get the number
1145258561, or 0x44434241 - this is probaby what the original poster meant - _converted_ to characters, a number will take more space.
But any sequence of bytes can be seen as anything, char, float, char*, struct foo*,

So, when a network protocol sends 'characters'- you can make those characters mean anything you want, put two together, and you have a 16 bit short, etc.
But beware of how your machine stores integers!
The bytes above may have looked like
"BADC" on some machines to give the same number!
If I am not mistaken, I think the IBM PC is one of them ...

Look at
man htonl
man htons

Then you will understand why it just sends 'characters'- to make it an 'int'you use one of those functions, that puts the bytes in the right 'network'order, and put them back to local 'machine' order.

I often randomize characters with the % operator.

rand() % 10 gives 0-9

rand() % ('z'-'a') + 'a' is also an interesting contruct

Some fun: Remember that characters, int, floats and all that are really just bits.
If you want char is a certain range, maybe you can just chop off some bits!
0123456701234567
1001010011101011
0000111110000000 <- maybe you just want those
-----------------
0000010010000000

This would make it fast by just using one machine op,

AND byte, 000011111