binary (?) to string

binary (?) to string

am 12.12.2006 21:16:30 von John DePasquale

This is a multi-part message in MIME format.

--===============0082201791==
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C71E2A.68E760AF"

This is a multi-part message in MIME format.

------_=_NextPart_001_01C71E2A.68E760AF
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

hi all,

I'm querying a binary field in MS-SQL using perl, and having a harder
time than I anticipated in converting it to string.

an example of the value in the SQL database looks like:
0x0000000000000112

=20

when I query the database and get a result set that includes that value
and print it from perl, I get unprintable characters.

when I try to query that same database again using the value as perl
sees it, it fails.=20

=20

example:

"select binary_field from table" -----> the value I get here and store
in the variable $c is unprintable.

=20

if I then try "select * from table where binary_field =3D $c" or "select =
*
from table where binary_field =3D " . $c, both fail.

=20

if I store '0x' . '0000000000000112' to $c and query for that value
against the binary field, it finds the record successfully.

=20

I'm assuming perl can convert the unprintable value of
0x0000000000000112 to a string looking like '0x0000000000000112', or
perhaps '0000000000000112', but having no luck finding it.

=20

is there such a function?

=20

thanks.

=20

John DePasquale

IT Manager

Law Offices of James Sokolove

1340 Centre Street - Suite 102

Newton, MA 02459

P: 617-742-0696, ext. 432

F: 617-964-4336

This email is intended solely for the use of the individual to whom it
is addressed and may contain information that is privileged,
confidential or otherwise exempt from disclosure under applicable law.
If the reader of this email is not the intended recipient or the
employee or agent responsible for delivering the message to the intended
recipient, you are hereby notified that any dissemination, distribution,
or copying of this communication is strictly prohibited. If you have
received this communication in error, please immediately notify us by
telephone and return the original message to us at the listed email
address. Thank You.

=20


------_=_NextPart_001_01C71E2A.68E760AF
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:st1=3D"urn:schemas-microsoft-com:office:smarttags" =
xmlns=3D"http://www.w3.org/TR/REC-html40">


charset=3Dus-ascii">

namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"place"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"PostalCode"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"State"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"City"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"Street"/>
namespaceuri=3D"urn:schemas-microsoft-com:office:smarttags"
name=3D"address"/>









style=3D'font-size:10.0pt;
font-family:Arial'>hi all,



style=3D'font-size:10.0pt;
font-family:Arial'>I’m querying a binary field in MS-SQL using =
perl, and
having a harder time than I anticipated in converting it to =
string.



style=3D'font-size:10.0pt;
font-family:Arial'>an example of the value in the SQL database looks =
like: 0x0000000000000112



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>when I query the database and get a result set that =
includes
that value and print it from perl, I get unprintable =
characters.



style=3D'font-size:10.0pt;
font-family:Arial'>when I try to query that same database again using =
the value
as perl sees it, it fails.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>example:



style=3D'font-size:10.0pt;
font-family:Arial'>“select binary_field from table” =
---
size=3D2 face=3DWingdings> style=3D'font-size:10.0pt;font-family:Wingdings'>à ont
size=3D2 face=3DArial> style=3D'font-size:10.0pt;font-family:Arial'> the value I
get here and store in the variable $c is =
unprintable.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>if I then try “select * from table where =
binary_field
=3D $c” or “select * from table where binary_field =3D =
“ . $c, both
fail.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>if I store ‘0x’ . =
‘0000000000000112’
to $c and query for that value against the binary field, it finds the =
record
successfully.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>I’m assuming perl can convert the unprintable =
value of
0x0000000000000112 to a string looking like =
‘0x0000000000000112’,
or perhaps ‘0000000000000112’, but having no luck finding =
it.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>is there such a =
function?



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>thanks.



style=3D'font-size:
12.0pt'> 



Roman"> style=3D'font-size:10.0pt;color:blue'>John =
DePasquale



Roman"> style=3D'font-size:10.0pt;color:blue'>IT =
Manager



Roman"> style=3D'font-size:10.0pt;color:blue'>Law Offices of James =
Sokolove



style=3D"BACKGROUND-POSITION: left bottom; BACKGROUND-IMAGE: =
url(res://ietag.dll/#34/#1001); BACKGROUND-REPEAT: repeat-x"
w:st=3D"on"> style=3D'font-size:10.0pt;color:blue'>1340 Centre =
Street
size=3D2 color=3Dblue> =
tabIndex=3D"0"
style=3D"BACKGROUND-POSITION: left bottom; BACKGROUND-IMAGE: =
url(res://ietag.dll/#34/#1001); BACKGROUND-REPEAT: repeat-x"
w:st=3D"on">Suite =
102



size=3D2
color=3Dblue face=3D"Times New Roman"> style=3D'font-size:10.0pt;color:blue'>Newton
t
size=3D2 color=3Dblue>, =
w:st=3D"on">MA w:st=3D"on">02459
p>

Roman"> style=3D'font-size:10.0pt;color:blue'>P: 617-742-0696, ext. =
432



Roman"> style=3D'font-size:10.0pt;color:blue'>F: =
617-964-4336



style=3D'font-size:7.5pt;font-family:"Microsoft Sans Serif"'>This email =
is
intended solely for the use of the individual to whom it is addressed =
and may
contain information that is privileged, confidential or otherwise exempt =
from
disclosure under applicable law. If the reader of this email is not the
intended recipient or the employee or agent responsible for delivering =
the
message to the intended recipient, you are hereby notified that any
dissemination, distribution, or copying of this communication is =
strictly
prohibited. If you have received this communication in error, please
immediately notify us by telephone and return the original message to us =
at the
listed email address. Thank You.
face=3D"Microsoft Sans Serif"> style=3D'font-size:7.5pt;font-family:"Microsoft Sans =
Serif"'>



style=3D'font-size:
12.0pt'> 









------_=_NextPart_001_01C71E2A.68E760AF--

--===============0082201791==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0082201791==--

Re: binary (?) to string

am 12.12.2006 22:16:14 von dbecoll

John DePasquale wrote:

> hi all,
> =

> I=92m querying a binary field in MS-SQL using perl, and having a harder
> time than I anticipated in converting it to string.
> =

> an example of the value in the SQL database looks like: 0x0000000000000112
> =

> =

> =

> when I query the database and get a result set that includes that value
> and print it from perl, I get unprintable characters.
> =

> when I try to query that same database again using the value as perl
> sees it, it fails.
> =

> =

> =

> example:
> =

> =93select binary_field from table=94 ---=E0 the value I get here and stor=
e in
> the variable $c is unprintable.
> =

> =

> =

> if I then try =93select * from table where binary_field =3D $c=94 or =93s=
elect *
> from table where binary_field =3D =93 . $c, both fail.
> =

> =

> =

> if I store =910x=92 . =910000000000000112=92 to $c and query for that val=
ue
> against the binary field, it finds the record successfully.
> =

> =

> =

> I=92m assuming perl can convert the unprintable value of
> 0x0000000000000112 to a string looking like =910x0000000000000112=92, or
> perhaps =910000000000000112=92, but having no luck finding it.
> =

> =

> =

> is there such a function?

The problem is that you have a 64-bit integer there and it may not be suppo=
rted
on all machines.

printf "num=3D0x%16X\n", unpack 'Q', $c; # unpack 64-bit unsigned binary

On a 32-bit machine, you could try:

printf "num=3D0x%08X%08X\n", unpack 'LL', $c;

and it the words come out backwards you could play with 'V' and/or swap the
2 resultant 32-bit values.


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: binary (?) to string

am 13.12.2006 16:45:44 von John DePasquale

hi bill,
much thanks for the information. =

I think i'm pretty close. I was able to print the value correctly using:
printf "num=3D0x%08X%08X\n", unpack 'LN', $c;

which printed the correct output: num=3D0x0000000000046F1D

however, I need to store it to a variable, not just print it. how can I sto=
re the value 0x0000000000046F1D ( or at least that value minus the 0x ) to =
a variable using unpack ( or pack )?

John DePasquale
IT Manager
Law Offices of James Sokolove
1340 Centre Street - Suite 102
Newton, MA 02459
P: 617-742-0696, ext. 432
F: 617-964-4336
=

This email is intended solely for the use of the individual to whom it is a=
ddressed and may contain information that is privileged, confidential or ot=
herwise exempt from disclosure under applicable law. If the reader of this =
email is not the intended recipient or the employee or agent responsible fo=
r delivering the message to the intended recipient, you are hereby notified=
that any dissemination, distribution, or copying of this communication is =
strictly prohibited. If you have received this communication in error, plea=
se immediately notify us by telephone and return the original message to us=
at the listed email address. Thank You.

-----Original Message-----
From: $Bill Luebkert [mailto:dbecoll@adelphia.net] =

Sent: Tuesday, December 12, 2006 4:16 PM
To: John DePasquale
Cc: activeperl@listserv.ActiveState.com
Subject: Re: binary (?) to string

John DePasquale wrote:

> hi all,
> =

> I'm querying a binary field in MS-SQL using perl, and having a harder
> time than I anticipated in converting it to string.
> =

> an example of the value in the SQL database looks like: 0x0000000000000112
> =

> =

> =

> when I query the database and get a result set that includes that value
> and print it from perl, I get unprintable characters.
> =

> when I try to query that same database again using the value as perl
> sees it, it fails.
> =

> =

> =

> example:
> =

> "select binary_field from table" ---=E0 the value I get here and store in
> the variable $c is unprintable.
> =

> =

> =

> if I then try "select * from table where binary_field =3D $c" or "select *
> from table where binary_field =3D " . $c, both fail.
> =

> =

> =

> if I store '0x' . '0000000000000112' to $c and query for that value
> against the binary field, it finds the record successfully.
> =

> =

> =

> I'm assuming perl can convert the unprintable value of
> 0x0000000000000112 to a string looking like '0x0000000000000112', or
> perhaps '0000000000000112', but having no luck finding it.
> =

> =

> =

> is there such a function?

The problem is that you have a 64-bit integer there and it may not be suppo=
rted
on all machines.

printf "num=3D0x%16X\n", unpack 'Q', $c; # unpack 64-bit unsigned binary

On a 32-bit machine, you could try:

printf "num=3D0x%08X%08X\n", unpack 'LL', $c;

and it the words come out backwards you could play with 'V' and/or swap the
2 resultant 32-bit values.


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: binary (?) to string

am 13.12.2006 17:24:36 von dbecoll

John DePasquale wrote:

> hi bill,
> much thanks for the information.
> I think i'm pretty close. I was able to print the value correctly using:
> printf "num=0x%08X%08X\n", unpack 'LN', $c;
>
> which printed the correct output: num=0x0000000000046F1D
>
> however, I need to store it to a variable, not just print it. how can I store the value 0x0000000000046F1D ( or at least that value minus the 0x ) to a variable using unpack ( or pack )?

If you have 64-bit integers, you can unpack into a vrbl - otherwise,
you could use something like Math::BigInt to manipulate it.

use Math::BigInt;

# simulate binary number coming in in network order:
my $c = pack 'NN', 0x00000001, 0x00000112;

my ($m, $l) = unpack 'NN', $c;
my $ms = Math::BigInt->new($m);
my $ls = Math::BigInt->new($l);
my $value = Math::BigInt::numify (($ms << 32) | $ls);
print "$value\n";

__END__

Using a string in the DB would be cleaner endian-wise - you could do
something like this:

use Math::BigInt;

# assuming you have a string like this from your query:
my $Str = '0000000000000112';

my $msStr = substr $Str, 0, 8;
my $lsStr = substr $Str, 8, 8;
my $ls = Math::BigInt->new(hex $lsStr);
my $ms = Math::BigInt->new(hex $msStr);
my $value = Math::BigInt::numify (($ms << 32) | $ls);
print "$value\n";
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: binary (?) to string

am 13.12.2006 18:09:12 von John DePasquale

hi bill,
thanks again. please forgive my lack of knowledge here. how do you
unpack into a variable? I've tried tracking down that answer but have
struck out.

John DePasquale
IT Manager
Law Offices of James Sokolove
1340 Centre Street - Suite 102
Newton, MA 02459
P: 617-742-0696, ext. 432
F: 617-964-4336

This email is intended solely for the use of the individual to whom it
is addressed and may contain information that is privileged,
confidential or otherwise exempt from disclosure under applicable law.
If the reader of this email is not the intended recipient or the
employee or agent responsible for delivering the message to the intended
recipient, you are hereby notified that any dissemination, distribution,
or copying of this communication is strictly prohibited. If you have
received this communication in error, please immediately notify us by
telephone and return the original message to us at the listed email
address. Thank You.

-----Original Message-----
From: $Bill Luebkert [mailto:dbecoll@adelphia.net]
Sent: Wednesday, December 13, 2006 11:25 AM
To: John DePasquale
Cc: activeperl@listserv.ActiveState.com
Subject: Re: binary (?) to string

John DePasquale wrote:

> hi bill,
> much thanks for the information.
> I think i'm pretty close. I was able to print the value correctly
using:
> printf "num=0x%08X%08X\n", unpack 'LN', $c;
>
> which printed the correct output: num=0x0000000000046F1D
>
> however, I need to store it to a variable, not just print it. how can
I store the value 0x0000000000046F1D ( or at least that value minus the
0x ) to a variable using unpack ( or pack )?

If you have 64-bit integers, you can unpack into a vrbl - otherwise,
you could use something like Math::BigInt to manipulate it.

use Math::BigInt;

# simulate binary number coming in in network order:
my $c = pack 'NN', 0x00000001, 0x00000112;

my ($m, $l) = unpack 'NN', $c;
my $ms = Math::BigInt->new($m);
my $ls = Math::BigInt->new($l);
my $value = Math::BigInt::numify (($ms << 32) | $ls);
print "$value\n";

__END__

Using a string in the DB would be cleaner endian-wise - you could do
something like this:

use Math::BigInt;

# assuming you have a string like this from your query:
my $Str = '0000000000000112';

my $msStr = substr $Str, 0, 8;
my $lsStr = substr $Str, 8, 8;
my $ls = Math::BigInt->new(hex $lsStr);
my $ms = Math::BigInt->new(hex $msStr);
my $value = Math::BigInt::numify (($ms << 32) | $ls);
print "$value\n";
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: binary (?) to string

am 13.12.2006 20:19:19 von dbecoll

John DePasquale wrote:

> hi bill,
> thanks again. please forgive my lack of knowledge here. how do you
> unpack into a variable? I've tried tracking down that answer but have
> struck out.

There was an example below:
my ($m, $l) = unpack 'NN', $c;
just change it to
my $v = unpack 'Q', $c;
for quad machines (untested).

In all these cases, you have to worry about byte/word order and
adjust for your endian type.

> John DePasquale wrote:
>
>
>>hi bill,
>>much thanks for the information.
>>I think i'm pretty close. I was able to print the value correctly
>
> using:
>
>> printf "num=0x%08X%08X\n", unpack 'LN', $c;
>>
>>which printed the correct output: num=0x0000000000046F1D
>>
>>however, I need to store it to a variable, not just print it. how can
>
> I store the value 0x0000000000046F1D ( or at least that value minus the
> 0x ) to a variable using unpack ( or pack )?
>
> If you have 64-bit integers, you can unpack into a vrbl - otherwise,
> you could use something like Math::BigInt to manipulate it.
>
> use Math::BigInt;
>
> # simulate binary number coming in in network order:
> my $c = pack 'NN', 0x00000001, 0x00000112;
>
> my ($m, $l) = unpack 'NN', $c;
> my $ms = Math::BigInt->new($m);
> my $ls = Math::BigInt->new($l);
> my $value = Math::BigInt::numify (($ms << 32) | $ls);
> print "$value\n";
>
> __END__
>
> Using a string in the DB would be cleaner endian-wise - you could do
> something like this:
>
> use Math::BigInt;
>
> # assuming you have a string like this from your query:
> my $Str = '0000000000000112';
>
> my $msStr = substr $Str, 0, 8;
> my $lsStr = substr $Str, 8, 8;
> my $ls = Math::BigInt->new(hex $lsStr);
> my $ms = Math::BigInt->new(hex $msStr);
> my $value = Math::BigInt::numify (($ms << 32) | $ls);
> print "$value\n";
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs