How can I print a number larger than 128?

Discussion in 'Perl Misc' started by kun niu, Dec 8, 2007.

  1. kun niu

    kun niu Guest

    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.
    kun niu, Dec 8, 2007
    #1
    1. Advertising

  2. kun niu

    Guest

    kun niu <> wrote in message-id: <>

    >
    > 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;
    , Dec 8, 2007
    #2
    1. Advertising

  3. kun niu

    smallpond Guest

    On Dec 8, 8:32 am, kun niu <> wrote:
    > 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
    smallpond, Dec 8, 2007
    #3
  4. kun niu

    Doug Miller Guest

    In article <Jzy6j.1032$bW.927@trnddc07>, d wrote:
    >
    >kun niu <> wrote in message-id:
    > <>
    >
    >>
    >> 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.


    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.

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
    Doug Miller, Dec 8, 2007
    #4
  5. kun niu

    Guest

    (Doug Miller) wrote in message-id: <6bA6j.5046$>

    >
    > In article <Jzy6j.1032$bW.927@trnddc07>, d wrote:
    > >
    > >kun niu <> wrote in message-id:
    > > <>
    > >
    > >>
    > >> 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.

    >
    > 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.
    >


    cause the OP says that he wishes to send _only one byte_
    , Dec 8, 2007
    #5
  6. On Sat, 08 Dec 2007 19:33:41 +0000, QoS wrote:

    > (Doug Miller) wrote in message-id:
    > <6bA6j.5046$>
    >
    >
    >> In article <Jzy6j.1032$bW.927@trnddc07>, d wrote:
    >> >
    >> >kun niu <> wrote in message-id:
    >> > <>
    >> >
    >> >
    >> >> 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.

    >>
    >> 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.
    >>
    >>

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


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

    M4
    Martijn Lievaart, Dec 8, 2007
    #6
  7. kun niu

    John Bokma Guest

    Martijn Lievaart <> wrote:

    > 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.

    --
    John

    http://johnbokma.com/
    John Bokma, Dec 8, 2007
    #7
  8. kun niu

    Doug Miller Guest

    In article <pkC6j.10305$OR.7977@trnddc01>, d wrote:
    >
    > (Doug Miller) wrote in message-id:
    > <6bA6j.5046$>
    >
    >>
    >> In article <Jzy6j.1032$bW.927@trnddc07>, d wrote:
    >> >
    >> >kun niu <> wrote in message-id:
    >> > <>
    >> >
    >> >>
    >> >> 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.

    >>
    >> 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.
    >>

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


    And your point would be....?

    255 > 128, last time I checked.



    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
    Doug Miller, Dec 9, 2007
    #8
  9. On Sun, 09 Dec 2007 22:25:59 +0000, Doug Miller wrote:

    >>> >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.
    >>>
    >>>

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

    > 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
    Martijn Lievaart, Dec 9, 2007
    #9
  10. kun niu

    Doug Miller Guest

    In article <>, Martijn Lievaart <> wrote:
    >On Sun, 09 Dec 2007 22:25:59 +0000, Doug Miller wrote:
    >
    >>>> >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.
    >>>>
    >>>>
    >>>cause the OP says that he wishes to send _only one byte_
    >>>
    >>>

    >> 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.


    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.

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
    Doug Miller, Dec 10, 2007
    #10
  11. kun niu

    Guest

    (Doug Miller) wrote in message-id: <dH97j.76990$>

    >
    > In article <>, Martijn Lievaart <> wrote:
    > >On Sun, 09 Dec 2007 22:25:59 +0000, Doug Miller wrote:
    > >
    > >>>> >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.
    > >>>>
    > >>>>
    > >>>cause the OP says that he wishes to send _only one byte_
    > >>>
    > >>>
    > >> 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.

    >
    > 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
    , Dec 10, 2007
    #11
  12. kun niu

    TonyV Guest

    On Dec 10, 10:07 am, wrote:
    > (Doug Miller) wrote in message-id: <dH97j.76990$>
    >
    > > In article <>, Martijn Lievaart <> wrote:
    > > >On Sun, 09 Dec 2007 22:25:59 +0000, Doug Miller wrote:

    >
    > > >>>> >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.

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

    >
    > > >> 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.

    >
    > > 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


    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.
    TonyV, Dec 10, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Neuroth
    Replies:
    4
    Views:
    2,131
    Roedy Green
    Apr 19, 2004
  2. Peter Williams
    Replies:
    1
    Views:
    1,351
    Dylan Parry
    Jun 3, 2005
  3. Peter Knoerrich
    Replies:
    9
    Views:
    280
    Peter Knoerrich
    Jul 1, 2004
  4. Eric Sosman

    Re: Integer 128 != Integer 128 ??

    Eric Sosman, Oct 12, 2010, in forum: Java
    Replies:
    6
    Views:
    816
    Screamin Lord Byron
    Oct 13, 2010
  5. chankey pathak

    Re: Integer 128 != Integer 128 ??

    chankey pathak, Oct 13, 2010, in forum: Java
    Replies:
    0
    Views:
    828
    chankey pathak
    Oct 13, 2010
Loading...

Share This Page