Am I missing something about BER-compressed integer?

All:

The following:

p [9999].pack(“w”)

gives:

“\316\017”

but I expected to get:

“\047\017”

Notice the correct bit pattern for the upper octet (100111) appears in
the octet generated by Ruby (11001110) but shifted and with an extra
bit at the top. Is this a bug in pack(“w”), or is my expectation
wrong?

Thanks,
-f

Need an advice please,

I have an error while trying to connect ruby to
oracle.
u can see the error below.What should I do ?

E:\ruby\bin>ruby -r oci8 -e “OCI8.new(‘hr’, ‘hr’,
//10.10.xx.xx:1521/oradbx’).
exec(‘select * from JOBS’) do |r| puts r.join(’,’);
end”
E:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:49:in `create’:
ERROR (OCIError)
from E:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:49

thnx
andre

andre hartawan wrote:

E:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:49:in `create’:
ERROR (OCIError)
from E:/ruby/lib/ruby/site_ruby/1.8/oci8.rb:49

  1. Have you been able to connect to Oracle using this method before now?
    In
    other words, is this your first effort to connect, or is this the first
    time you have had a problem connecting?

  2. Is the Oracle server running on the provided address and port?

  3. Is the provided SQL accepted when using another way to talk to
    Oracle?

  4. Have you considered putting your Ruby code in a script, which would
    give
    you more control over what happens and how errors are reported?

Andre,

     The problem occured at "OCIEnv.create(OCI_OBJECT);

oci8_env_s_create;" when loading ruby-oci8 driver, before connecting to
the db server.
Firstly you can check what Paul suggested. I think your codes
are correct, which have been validated on my machine. Besides, I’d like
to ask you the following:

  1. Is your OracleXE DB loacated on the machine from where you ran your
    codes? If not, have you installed Oracle Instant Client on the machine
    from where you ran your codes?
  2. Can you tell us what the sizes of your file
    E:/ruby/lib/ruby/site_ruby/1.8/oci8.rb and file
    E:/ruby/lib/ruby/site_ruby/1.8/oci8lib.so are? Also the exact version of
    your ruby-oci8, and the exact version of your OracleXE DB server? To try
    reinstalling your ruby-oci8 driver is a method too.
    Hope you can succeed.

Shiwei
The views expressed are my own and not necessarily those of Oracle and
its affiliates.

On 12/8/06, Francis C. [email protected] wrote:

but I expected to get:

I figured it out. There’s no bug in Ruby. pack(“w”) produces the
integer representation that’s occasionally used in BER to save space
when representing things like OIDs, not the “standard” BER
representation that requires a field-size prefix.

No, it is not my first time use oracle
connection,actually i have used TOAD and It’s working
properlly,but it’s my first time using ruby,du u have
any suggestion ?

andre hartawan wrote:

[snip thread hijack]

Please, don’t use the “Reply” function of your email client to start
talking about a topic completely different than the one of the post
you’re replying to, but write a new message from scratch. It Makes
Things Confusing ™.

Disclaimer: if it’s the apparent fault of my client and noone else sees
this as an (accidental) hijack of Francis C.'s about BER, ignore
this message.

David V.

Francis C. wrote:

On 12/8/06, Francis C. [email protected] wrote:

but I expected to get:

I figured it out. There’s no bug in Ruby. pack(“w”) produces the
integer representation that’s occasionally used in BER to save space
when representing things like OIDs, not the “standard” BER
representation that requires a field-size prefix.

Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page:

The pack code w has been added to support a portable binary data
encoding scheme that goes way beyond simple integers. A BER (Binary
Encoded Representation) compressed unsigned integer stores base 128
digits, most significant digit first, with as few digits as possible.
Bit eight (the high bit) is set on each byte except the last. There is
no size limit to BER encoding, but Perl won’t go to extremes.

Applying the above rules:

irb(main):045:0> printf("%08b%08b", 0b10000000 | (9999/128), 9999%128)
1100111000001111=> nil

Ruby:

irb(main):046:0> [9999].pack(“w”).each_byte { |b| printf("%08b", b) }
1100111000001111=> “\316\017”

andre hartawan wrote:

No, it is not my first time use oracle
connection,actually i have used TOAD and It’s working
properlly,but it’s my first time using ruby,du u have
any suggestion ?

Please re-read my first post. Is the SQL syntax you have submitted to
the
Ruby interface acceptable elsewhere? Is the address and the port
correct?
And so forth.

Basically, re-read my first post and run each of the tests I suggested
there.

On 12/9/06, Edwin F. [email protected] wrote:

Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page:

The pack code w has been added to support a portable binary data
encoding scheme that goes way beyond simple integers. A BER (Binary
Encoded Representation) compressed unsigned integer stores base 128
digits, most significant digit first, with as few digits as possible.
Bit eight (the high bit) is set on each byte except the last. There is
no size limit to BER encoding, but Perl won’t go to extremes.

Thanks for this info. BER is not specified by Perl, of course, but
rather by
the ITU. The only normative language I’ve found on the “BER-compressed
integer” encoding (that’s the language appearing in Ruby pack.c) is in
ITU/X.690, graf 8.19.2:

“Each subidentifier is represented as a series of (one or more) octets.
Bit
8 of each octet indicates whether it is the last in the series: bit 8 of
the
last octet is zero; bit 8 of each preceding octet is one. Bits 7 to 1 of
the
octets in the series collectively encode the subidentifier.
Conceptually,
these groups of bits are concatenated to form an unsigned binary number
whose most significant bit is bit 7 of the first octet and whose least
significant bit is bit 1 of the last octet. The subidentifier shall be
encoded in the fewest possible octets, that is, the leading octet of the
subidentifier shall not have
the value 80(16).”

But the “normal” BER encoding for integers is specified in graf 8.3.3 of
the
same document:

“The contents octets shall be a two’s complement binary number equal to
the
integer value, and consisting of bits 8 to 1 of the first octet,
followed by
bits 8 to 1 of the second octet, followed by bits 8 to 1 of each octet
in
turn up to and including the last octet of the contents octets.”

Array#pack doesn’t support this encoding as far as I know (I ended up
writing it by hand in the Net::BER module).

hi paul,

thnx for advice,at last my ruby can talk to oracle
after a long hard day.actualy I reinstall my oracle
client and erase all oracle registry in my pc.
then it’s work,once again thnx.

andre

Francis C. wrote:

On 12/9/06, Edwin F. [email protected] wrote:

Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page:

Thanks for this info. BER is not specified by Perl, of course, but
rather by
the ITU. The only normative language I’ve found on the "BER-compressed
(snip)
Array#pack doesn’t support this encoding as far as I know (I ended up
writing it by hand in the Net::BER module).

What I meant was not that Perl specified the BER encoding. I just meant
that Ruby’s implementation behavior is based on the Perl pack behavior,
which itself was based on a project at Casbah.org (which I could not
locate). They explicitly say that it’s not the BER from ASN.1. I don’t
know if that means it is or is not the one defined by ITU.