|
-
July 27th, 2009, 01:40 PM
#1
using something instead of char for recv?
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?
-
July 27th, 2009, 04:04 PM
#2
Re: using something instead of char for recv?
You can just cast the data to the type you need. Cast it to an unsigned char.
Viggy
-
July 27th, 2009, 04:17 PM
#3
Re: using something instead of char for recv?
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);
-
July 27th, 2009, 04:50 PM
#4
Re: using something instead of char for recv?
-
July 27th, 2009, 05:00 PM
#5
Re: using something instead of char for recv?
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
-
July 27th, 2009, 05:26 PM
#6
Re: using something instead of char for recv?
...receive chars over 256...
...as you know char has a range from -128 to 127...
Even as unsigned char, how could it exceed 255?
the program i have sends numbers like 0 1 2 3 4 5 (as chars)
Are these ASCII characters representing the digits?
Are these binary values you've listed?
but when i use that int, it just comes a bunch of numbers
This is why I post so many questions. It takes some considerable care to communicate the details, how about some code?
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
Code:
char buffer[] = "012345";
unsigned char *p = (unsigned char *)buffer;
Excuse the C casting here, this is pseudo code for discussion.
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.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
July 27th, 2009, 06:08 PM
#7
Re: using something instead of char for recv?
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.).
Last edited by HouseMD93; July 27th, 2009 at 06:14 PM.
-
July 28th, 2009, 05:00 AM
#8
Re: using something instead of char for recv?
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.
-
July 28th, 2009, 08:13 AM
#9
Re: using something instead of char for recv?
 Originally Posted by HouseMD93
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):
Code:
int *p=(int *)buffer;
printf("%d %d %d %d",p[0],p[1],p[2],p[3]);
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.
-
July 28th, 2009, 10:35 AM
#10
Re: using something instead of char for recv?
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 :/
Last edited by HouseMD93; July 28th, 2009 at 11:05 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|