[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

unpack 4 bytes to a signed integer

w wg

1/15/2007 1:30:00 PM

Hi
I' m using unpack to convert 4 bytes to local integer, but ruby just
supply the "N" modifer which means unsigned long integer.

My questions is :
How to unpack 4 bytes to a signed integer ?

Thank you.

--
wwg

13 Answers

Mike Stok

1/15/2007 2:20:00 PM

0


On 15-Jan-07, at 8:29 AM, w wg wrote:

> Hi
> I' m using unpack to convert 4 bytes to local integer, but ruby just
> supply the "N" modifer which means unsigned long integer.
>
> My questions is :
> How to unpack 4 bytes to a signed integer ?


There are "i" and "I" for signed and unsigned integer respectively,
which deals in the local size of integer.

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





w wg

1/15/2007 3:10:00 PM

0

But the "i" and "I" modifer don't care bytes order.
I'm reading data from network using IO#sysread, I need receive integer
, not unsigned integer.

Thank you for your reply.

2007/1/15, Mike Stok <mike@stok.ca>:
>
> On 15-Jan-07, at 8:29 AM, w wg wrote:
>
> > Hi
> > I' m using unpack to convert 4 bytes to local integer, but ruby just
> > supply the "N" modifer which means unsigned long integer.
> >
> > My questions is :
> > How to unpack 4 bytes to a signed integer ?
>
>
> There are "i" and "I" for signed and unsigned integer respectively,
> which deals in the local size of integer.
>
> Hope this helps,
>
> Mike
>
> --
>
> Mike Stok <mike@stok.ca>
> http://www.stok...
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
>


--
--
WenGe Wang

Erik Veenstra

1/15/2007 3:24:00 PM

0

s = 0xFF.chr * 4
i = s.unpack("L").shift

p i
p 2**32-1


Erik Veenstra

1/15/2007 3:27:00 PM

0

s = 0xFF.chr * 4
i = s.unpack("L").shift

p i
p 2**32-1


Mike Stok

1/15/2007 3:49:00 PM

0


On 15-Jan-07, at 10:10 AM, w wg wrote:

> But the "i" and "I" modifer don't care bytes order.
> I'm reading data from network using IO#sysread, I need receive integer
> , not unsigned integer.
>
> Thank you for your reply.

OK then, what about "V"? "treat 4 characters as an unsigned long in
little-endian byte order"

The N uses network byte order "big-endian"

Mike


>
> 2007/1/15, Mike Stok <mike@stok.ca>:
>>
>> On 15-Jan-07, at 8:29 AM, w wg wrote:
>>
>> > Hi
>> > I' m using unpack to convert 4 bytes to local integer, but ruby
>> just
>> > supply the "N" modifer which means unsigned long integer.
>> >
>> > My questions is :
>> > How to unpack 4 bytes to a signed integer ?
>>
>>
>> There are "i" and "I" for signed and unsigned integer respectively,
>> which deals in the local size of integer.
>>
>> Hope this helps,
>>
>> Mike
>>
>> --
>>
>> Mike Stok <mike@stok.ca>
>> http://www.stok...
>>
>> The "`Stok' disclaimers" apply.
>>
>>
>>
>>
>>
>>
>
>
> --
> --
> WenGe Wang
>
>

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





Joel VanderWerf

1/15/2007 8:40:00 PM

0

w wg wrote:
> Hi
> I' m using unpack to convert 4 bytes to local integer, but ruby just
> supply the "N" modifer which means unsigned long integer.
>
> My questions is :
> How to unpack 4 bytes to a signed integer ?

The best way I've found is to unpack with N (to get the swapping right)
and then do some arithmetic to interpret the unsigned value as signed:

x = -123
s = [x].pack("N")
# Note that for _pack_ there is no need for a
# special signed version of N

p s # ==> "\377\377\377\205"

length = 32
mid = 2**(length-1)
max_unsigned = 2**length
to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}

p to_signed[s.unpack("N").first] # ==> -123



This is all very hard for me to remember, so I've written a library to
do it, bit-struct (http://redshift.sourceforge.net/...). This
makes life easier:

require 'bit-struct'

class Packet < BitStruct
signed :x, 32
end

pkt = Packet.new
pkt.x = -123

p pkt.to_s # ==> "\377\377\377\205"
p pkt.x # ==> -123

# given string data from a network:
pkt2 = Packet.new("\377\377\377\205")
p pkt2.x # ==> -123

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

w wg

1/16/2007 1:42:00 PM

0

I tried your codes, it works exactly as what I expected. I havn't
tried your library, but I think it should get the correct result too.

Thank you.

> The best way I've found is to unpack with N (to get the swapping right)
> and then do some arithmetic to interpret the unsigned value as signed:
>
> x = -123
> s = [x].pack("N")
> # Note that for _pack_ there is no need for a
> # special signed version of N
>
> p s # ==> "\377\377\377\205"
>
> length = 32
> mid = 2**(length-1)
> max_unsigned = 2**length
> to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}
>
> p to_signed[s.unpack("N").first] # ==> -123

Hachiroku ????

9/16/2010 6:36:00 PM

0


"(?`?.??Craig Chilton??.???) <http://www.LayoffRemedy.com... <x@p.com> wrote
in message news:h4k496lqehlo5fsi90jjhadd6jhdes3evu@4ax.com...
> X-No-Archive: no
>
> On Thu, 16 Sep 2010 10:34:43 -0500,
> Dakota <markp@NOSPAMmail.com> wrote:
>> (?`?.??Craig Chilton??.???) <http://www.LayoffReme... wrote:

>> The election of America's first non-white President has helped to
>> expose the bigots. It's a step in the right direction.
>
> That was a HUGE victory for FAIRNESS in America!!
====
Obama has done more to stir up racial hatred than any president we have ever
had. Bush was bad, but Obama has screwed things up so much and accomplished
so little, the it sets back niggers immesurably. Not only is he blamed for
being a nigger and a socialist, but for his attitude toward Islamic
intrusions on the American way of life.

>
>

archie dux

9/16/2010 6:42:00 PM

0

On Sep 16, 11:35 am, "Jose SixPak" <anonym...@not-for-mail.invalid>
wrote:
> "(¯`·.¸¸Craig Chilton¸¸.·´¯) <http://www.LayoffRemedy.com... <x...@p.com> wrote
> in messagenews:h4k496lqehlo5fsi90jjhadd6jhdes3evu@4ax.com...
>
> > X-No-Archive: no
>
> > On Thu, 16 Sep 2010 10:34:43 -0500,
> > Dakota <ma...@NOSPAMmail.com> wrote:
> >>  (¯`·.¸¸Craig Chilton¸¸.·´¯) <http://www.LayoffReme... wrote:
> >>     The election of America's first non-white President has helped to
> >> expose the bigots. It's a step in the right direction.
>
> >   That was a HUGE victory for FAIRNESS in America!!
>
> ====

> Obama has done more to stir up racial hatred than any president we have ever
> had.  Bush was bad, but Obama has screwed things up so much and accomplished
> so little, the it sets back niggers immesurably.  Not only is he blamed for
> being a nigger and a socialist, but for his attitude toward Islamic
> intrusions on the American way of life.

Gee, I'm going to go out on a limb here and hazard
a guess that in you case, it didn't take an Obama
to account for your own "racial hatred".


archie




>
>

pnyikos

9/17/2010 2:38:00 AM

0

On Sep 16, 8:00 pm, "(¯`·.¸¸Craig Chilton¸¸.·´¯) <http://
www.LayoffRemedy.com>" <x...@p.com> wrote:
> X-No-Archive: no
>
> On Thu, 16 Sep 2010 14:36:13 -0700 (PDT),
>
> pnyikos <nyik...@bellsouth.net> wrote:
> >  (¯`·.¸¸Craig Chilton¸¸.·´¯)  <http://www.LayoffReme... wrote:
> >>  Dakota <ma...@NOSPAMmail.com> wrote:
> >>>  (¯`·.¸¸Craig Chilton¸¸.·´¯) <http://www.LayoffReme... wrote:
> >>>>    Egalitarians KNOW that Almost All Bigots are INCORRIGIBLE. ...
> >     Especially leftist bigots, who are incorrigible because they are too
> > self-righteous to be anything BUT incorrigible, except by fellow
> > leftists, preferably to the left of them [as in "Pas d'ennemie a la
> > gauche!"].
>
>       Your apparent inability to do other than WHINE because social
> liberals tend to be self-assured

WHINE: an inchoate concept as used on Usenet;
were it made logically consistent and defined
broadly enough to encompass the most influential
uses in talk.abortion, talk.origins and alt.abortion,
it would mean "anything that can be construed,
in however strained a way, as a complaint,"
and hence would encompass much or all
of each of the following:
the Declaration of Independence, the Communist
Manifesto, Martin Luther King's "I have a
dream" speech, Mark Antony's funeral oration
in Shakespeare's _Julius Caesar_,
John the Baptist's denunciation of
Herod Antipas, and Jesus's "woe to you, scribes
and Pharisees, hypocrites" rant [more at RANT].

> >    "Liberals do a great deal of talking about hearing other points of
> > view, but it sometimes shocks them to learn that there are other
> > points of view."
> >                                ~ William F. Buckley
>
>      Buckley wasn't any better that YOU are at specifically identifying
> ANYTHING that social liberals do or advocate that is DETRIMENTAL to
> anyone.

Your self-serving "anyone" excludes cruelty to animals as well as to
unborn human beings. But even with that handicap, I have shown below,
with an ASSIST from you, that some of what they do IS detrimental to
some people.

> >>>>             By Exposing Bigots' Irrationality, We help NON-Bigots
> >>>>                                  to Avoid BECOMING Bigots.
> >     True enough, but of all people posting here, you are probably the
> > least qualified to do that, with the possible exception of W.T.S.  
>
>     He plays TROLL to you, and you take that seriously
> -- thus falling
> for his ploys hook, line, and sinker.

Does this mean you think he is an abortion foe trying to make pro-
choicers look bad by playing the part of a complete jerk and pro-
abortion fanatic?

[unsupported chest-thumping by Chilton deleted]

> <irrelevant comments about W.T.S. flushed>

They ARE relevant to your unsupported claim that he is trolling.

> >     Your constant [advocacy of] what a wonderful "REMEDY" abortion
> >  is...
>
>      ...*because* it is EXACTLY that -- with it ability to put the lives of
> 57,000,000 girls and women BACK on track, worldwide, annually,

or not, for those who come to regret their abortions, even to the
point of committing suicide or descending into drug dependency when
they realize that what they ended the life of was not an inanimate
"clump of tissue".

> >    ...and even taking Jesus's name in vain in conjunction with that....
>
>      That's something that I **never** do.

" The REMEDY of Abortion... and the *JESUS* of SECOND CHANCES"
is what you kept posting in somee of your articles.

You went on to post defamatory drivel about how most pro-lifers like
would have probably stoned Jesus for being merciful to the alleged
adulteress.

> [ ... ]
>
> >>>>      --  Slavery                                            --  EXTINCT
> >>>>      --  Opposition to Women's Suffrage       --  EXTINCT
> >>>>      --  Prohibition                                          --  EXTINCT
> >>>>      --  Opposition to Birth-Control Pill       --  EXTINCT
> >     Wrong, because it can also serve as an abortifacient, it is very
> > much opposed...
>
>    By **nitwits**!

So much for the opposition being "EXTINCT", EH?

>  NOT by INTELLIGENT and SENSIBLE people.  
> These days, society very properly regard opponents of contraception
> to be the LAUGHINGSTOCKS that they truly ARE.

Don't project your bigoted attitude on society.

> >    ...including  by some pharmacists whose consciences will not
> > allow them to dispense it.
>
>      ALL of those LOUSES should LOSE their licenses!  Permanently!!
>
>      I would hate to see such IGNORANT people in the position of
> dispensing controlled medicines.  That would be like allowing a
> 14-year-old who'd never been in a cockpit to fly an airliner.

More like allowing a pharmacist who is opposed to capital punishment
keep his job despite his unwillingness to dispense the poison used for
administering lethal injections to prisoners condemned to death.

YOU many not agree that there is any comparison between these two
cases, but that is YOUR opinion, and you are trying to impose YOUR
religion on pharmacists in a way that is PALPABLY detrimental to many
of them:

> >      And [sensible people] like you think they deserve to lose
> > their jobs on account of that, even if they refer the customers
> > to other pharmacies, don't you?
>
>     See above.  YES!!!  I will be very HAPPY when that becomes
> MANDATED.
>

Thus you betray your totalitarian bent, already beginning to be
evident in the way you said last year that you would impose censorship
on people with views which you PEREMPTORILY and ARBITRARILY call
"bigoted".

Concluded in next post.

Peter Nyikos