1

I am working with a system whereby I can send 1 32 bit int at a time. However I need to send two numbers at a time. What is the best way to do this in standard C?

I assume I will have to do some conversion to binary/hex and some masking?

Any ideas would be appreciated.

Thanks

14
  • A union would be one easy way to do this. Commented Mar 13, 2011 at 17:59
  • 1
    @David: No, unions store only one thing at a time. Commented Mar 13, 2011 at 18:01
  • What is the range of each number? How many bits do you need to send each number? Commented Mar 13, 2011 at 18:02
  • Do you mean "send over a network" or "send from one part of my program to another"? What's the range of possible values of the numbers you want to pack into a single 32-bit integer? Commented Mar 13, 2011 at 18:02
  • 1
    @delnan, I think David H. meant a union containing an int and a struct. Commented Mar 13, 2011 at 18:03

7 Answers 7

4

You can encode two 16-bit unsigned numbers like this:

unsigned int out = (in1 << 16) | in2;

and decode them like this:

unsigned int in1 = out >> 16;
unsigned int in2 = out & 0xFFFF;

All this assumes that int is at least 32 bits, that in1 and in2 are in the 0-65535 range, and that an unsigned int can be sent across correctly (w.r.t. endianness).

Sign up to request clarification or add additional context in comments.

8 Comments

That works as long as the sender and receiver have the same endianess. If that's going "on the wire", care must be taken (i.e. see the htons family of functions).
+1 But the +1 should go to Mat, because his caveat is more important than your solution (that is good too)... And perhaps you could explain that with "signed" ints all bets are off (because shift is undefined with integer < 0 )
The only data types I have at my disposal are int (32) and float (32) but i'll have a go now at making something like this work. Thanks!
endianess - a word I'm going to try and get into as many conversations as possible this week, for sport. :)
+1, if "the system" that already exists can sent a 32 bit int correctly, then it already accounts for endian-ness, and therefore this code will work regardless of the endian-ness of the sender or the recipient. If the existing system doesn't deal with endian-ness correctly, then that's a problem, but not with this answer. I suppose there's a slight niggle that this code doesn't work if the channel only does signed int, and the sending C implementation can't convert large unsigned values to signed. You'd create your unsigned int for sending, and now what? But that's rather rare.
|
2

If the numbers you are sending are between 0 and 65535 each just pack them in the 32-bit int you can send:

unsigned int n1 = 42;
unsigned int n2 = 1600;
unsigned int numbertosend = (n1 << 16) | n2;

On the receiving side, unpack the number

unsigned int n1 = receivednumber >> 16;
unsigned int n2 = receivednumber & 0xFFFF;

Comments

1

What range are your numbers? If they can fit in 16 bits, then you can pack two of those in your 32 bit int. Something like i = (n1 << 16) | (n2 &0xffff).

Comments

1

You could encode 2 16-bit numbers into a 32-bit number. For example:

int32 encode(int16 numA, int16 numb) {
    int 32 result = numA << 16 | numB;
    return  result;
}

int16 decodeNum1(int32 num) {
    return (num >> 16) & 0xFFFF;
}

int16 decodeNum2(int32 num) {
    return (num) & 0xFFFF;
}

Comments

1

If the two numbers you want to send are both prime, you could multiply them and send the product, and on the server factor them to get the result.

;p (inspired by the ambiguity in the question)

Comments

0

Assuming you want to send 2 16-bit numbers, you could probably do something like this:

unsigned int a, b, toSend;
// Give a and b a value...
toSend = ( a << 16 ) | b;

Comments

0

I think Unions in c would do the job.You could also go for Bit Fields in C

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.