I am using sockets to receive chars over 256 and as you know char has a range from -128 to 127. Theres my problem, i cannot use anything besides char to recv. Is there any way to convert it to another data variable?
Printable View
I am using sockets to receive chars over 256 and as you know char has a range from -128 to 127. Theres my problem, i cannot use anything besides char to recv. Is there any way to convert it to another data variable?
You can just cast the data to the type you need. Cast it to an unsigned char.
Viggy
char * is just a pointer to a byte buffer of any size you need. For instance, since an int is 4 bytes (on 32 bit platforms) you can send an int by casting it as a pointer to a 4 byte buffer:
Code:
int i=76000;
send(sock,(char *)&i,sizeof(int),0);
Cool... thanks =D
Problem: I cannot use it as an array? how can i get the data? as the program i have sends numbers like 0 1 2 3 4 5 (as chars). but when i use that int, it just comes a bunch of numbers
Even as unsigned char, how could it exceed 255?Quote:
...receive chars over 256...
...as you know char has a range from -128 to 127...
Are these ASCII characters representing the digits?Quote:
the program i have sends numbers like 0 1 2 3 4 5 (as chars)
Are these binary values you've listed?
This is why I post so many questions. It takes some considerable care to communicate the details, how about some code?Quote:
but when i use that int, it just comes a bunch of numbers
For example.
If the data you have is a character array containing digits in ascii 012345
Each digit would be a char or unsigned char if you so access them in the array that way. As in
Excuse the C casting here, this is pseudo code for discussion.Code:char buffer[] = "012345";
unsigned char *p = (unsigned char *)buffer;
At this point *p is an unsigned char, the character '0', the numeric value is 48 decimal.
Now, the first 4 characters, 0123, have the binary values of 48,49,50 and 51 in that order.
If I cast that to an int * and send it, what I'm sending is an integer made of those 4 values. Assuming an x86 processor, the integer will be 858927408.
If you were expecting to send a binary value of 12,345, you'd have to convert the string into a binary representation.
What I'm trying to convey is that I'm fairly sure we don't have enough data to understand your problem, and to offer solutions that make sense. We'll need more detail.
basically, it was sent like this (putting it in a printf way).
printf("%c%c%c%c",0,1,2,3) for example and each number means something, which the program is supposed to interpret.
ofcourse i used sprintf_s to put it into a buffer. (I understand that i cannot send higher than 256 with a char buffer, but basically i am accessing a program which i haven't created myself, so i do not know how they sent the code but i interpreted it using chars.).
I found that doing this: printf("%d %d %d %d",buffer) using int to receive it, would print out many numbers which are different, more specifically the other values received, so now it would be nice to know how i could use them, also it turns "m" into a rly rly rly rly large number.
This is horribly flawed. Basically the first %d is expecting an integer. It's getting the first four bytes of buffer and assuming that's an integer. The remaining 3 %d's are grabbing random memory on your stack and interpreting it as integers, but it (the memory) has no relation to "buffer".
If "buffer" is pointing to integer data in an array, a cast to int * might work (assuming the sending machine is little endian too):
In short you're leaving us with very few clues with which to deduce what it is you're trying to accomplish and your lack of C/C++ basics is probably the root of your problems.Code:
int *p=(int *)buffer;
printf("%d %d %d %d",p[0],p[1],p[2],p[3]);
That may be so, but i have given u enough clues. I used char buffer[size] for recv and used buffer[0] etc to interpret the message eg if (int)buffer[0] == 2 { eat_kellogs; }
One piece of information i am sending has a char larger than 256 and therefore i cannot interpret it.
I'll try what u showed me.
EDIT: It does work (of course) but i need to the values and ascii characters converted to that form, eg char 2 to that form is 1145330433 :/