How can I print a number larger than 128?

K

kun niu

Dear all,
I'm new here.
I've got a number larger than 128.
I want to send it by socket.
I find that if print the number directly, I'll print the number in
ascii format.
So I'll have 3 separate numbers transmitted.
But I only want to transmit one byte.
How can I do so?

Thanks for any help.
 
Q

QoS

kun niu said:
Dear all,
I'm new here.
I've got a number larger than 128.
I want to send it by socket.
I find that if print the number directly, I'll print the number in
ascii format.
So I'll have 3 separate numbers transmitted.
But I only want to transmit one byte.
How can I do so?

Thanks for any help.

You can try developing a compression algo or else use one off the shelf.
Something that reduces the need for consecutive zeros or ones.
The one byte constraint is more than a bit limiting though.

gl;
 
S

smallpond

Dear all,
I'm new here.
I've got a number larger than 128.
I want to send it by socket.
I find that if print the number directly, I'll print the number in
ascii format.
So I'll have 3 separate numbers transmitted.
But I only want to transmit one byte.
How can I do so?

Thanks for any help.


You should be able to send binary data to a socket using
syswrite(SOCK,$data,$length);
See packtut for information about packing data in network
byte order.
--S
 
D

Doug Miller

You can try developing a compression algo or else use one off the shelf.

Why would that be necessary?
Something that reduces the need for consecutive zeros or ones.

Why would that be necessary?
The one byte constraint is more than a bit limiting though.

Huh? One byte = 8 bits, which can hold values up to 2^8 - 1 = 255. All he
needs to do is send it in binary.
 
Q

QoS

Why would that be necessary?


Why would that be necessary?


Huh? One byte = 8 bits, which can hold values up to 2^8 - 1 = 255. All he
needs to do is send it in binary.

cause the OP says that he wishes to send _only one byte_
 
M

Martijn Lievaart

(e-mail address removed) (Doug Miller) wrote in message-id:

cause the OP says that he wishes to send _only one byte_

So? 128 is binary 10000000 which fits nicely in one byte.

M4
 
J

John Bokma

Martijn Lievaart said:
So? 128 is binary 10000000 which fits nicely in one byte.

128 is not larger than 128, so that's not the problem.

Anyway, up to (and including) 255 fits in one byte. If the OP needs to fit
all numbers in [0..max] with max > 255 in a single byte the OP has a
problem :). If the number of numbers is limited to max 256 different
ones, it's possible to encode each in a byte.
 
D

Doug Miller

(e-mail address removed) (Doug Miller) wrote in message-id:


cause the OP says that he wishes to send _only one byte_

And your point would be....?

255 > 128, last time I checked.
 
M

Martijn Lievaart

And your point would be....?

255 > 128, last time I checked.

Ah I see it now. The OP wants to send a number GREATER than 128. If that
number is < 256, one byte will do. Otherwise if < 384, just subtract 128
and send it in byte. Otherwise, without more information, it seems
impossible.

M4
 
D

Doug Miller

Ah I see it now. The OP wants to send a number GREATER than 128.

Wouldn't matter if he wanted to send a number less than 128. As long as it's
in the range 0..255, one byte is sufficient to express it. If it's out of that
range, it's not, and no compression algorithm is going to change that.
 
Q

QoS

Wouldn't matter if he wanted to send a number less than 128. As long as it's
in the range 0..255, one byte is sufficient to express it. If it's out of that
range, it's not, and no compression algorithm is going to change that.

Clocking.

Assume for a moment that the sending software and receiving software both
have hardcoded a _timing_ mechanism (asynchronous transmission).

Now the sending client starts the timer by sending its first bit, lets say
it is a zero, the sender will now not send any more bits until the value
of the bit is to change (lets say 14 cycles).

The receiver should now have a number like this, although it has received
only 1 bit. (the initial bit and 14 repetitions of that bit.)

0000000 00000000

Now the sender sends the next bit to indicate a change from 0 to 1.
Perhaps this time the sends waits 2 cycles after sending.

The reciever should now have a number like this, and we have sent
only 2 bits so far.

11 10000000 00000000

Some ethernet encodings work in a similar fashion to this.

Many more ways to do this.. gl
 
T

TonyV

Clocking.

Assume for a moment that the sending software and receiving software both
have hardcoded a _timing_ mechanism (asynchronous transmission).

Now the sending client starts the timer by sending its first bit, lets say
it is a zero, the sender will now not send any more bits until the value
of the bit is to change (lets say 14 cycles).

The receiver should now have a number like this, although it has received
only 1 bit. (the initial bit and 14 repetitions of that bit.)

0000000 00000000

Now the sender sends the next bit to indicate a change from 0 to 1.
Perhaps this time the sends waits 2 cycles after sending.

The reciever should now have a number like this, and we have sent
only 2 bits so far.

11 10000000 00000000

Some ethernet encodings work in a similar fashion to this.

Many more ways to do this.. gl

I think the user just wants to be able to send binary data. This
algorithm sounds way over the top of what he's asking.

In short, to send an integer using n bytes in a conventional manner,
the integer will need to be between 0 and 2^(8*n) - 1. One byte will
accommodate an integer between 0 and 255. Two bytes will accommodate
an integer between 0 and 65535. Three bytes will... Well, you get
the idea.

I think smallpond has the exact answer the OP is looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top