Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > use binary operator on ascii text string

Reply
Thread Tools

use binary operator on ascii text string

 
 
Sean.Dewis@gmail.com
Guest
Posts: n/a
 
      06-23-2006
Hi everyone

I'm pretty crap at perl, so I'd appreciate so help from you guys.

I have a string value held in $body variable.

What I need to do is manipulate each individual character value in the
string with OR - "|" and then replace that character with the
character's new value.

I'm using chr(ord($c) | 64) to get the new value, but I'm stuck on two
things: -

1) How to go through the string byte by byte and perform the OR 64 on
it
2) How to get the character equivalent back into the string in the
right place

For example the string is "abcdefg", by (I know it's not true)
performing OR 64 on each char I want "fghijkl" out.

Any idea's? Code examples would be appreciated.

TIA

Sean

 
Reply With Quote
 
 
 
 
krakle@visto.com
Guest
Posts: n/a
 
      06-23-2006

(E-Mail Removed) wrote:
> Hi everyone
>
> I'm pretty crap at perl,


And english

> Any idea's? Code examples would be appreciated.
>


http://www.perl.com

and

#!/usr/bin/perl -w
# ...

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      06-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi everyone
>
> I'm pretty crap at perl, so I'd appreciate so help from you guys.
>
> I have a string value held in $body variable.
>
> What I need to do is manipulate each individual character value in the
> string with OR - "|" and then replace that character with the
> character's new value.
>
> I'm using chr(ord($c) | 64) to get the new value, but I'm stuck on two
> things: -
>
> 1) How to go through the string byte by byte and perform the OR 64 on
> it
> 2) How to get the character equivalent back into the string in the
> right place
>
> For example the string is "abcdefg", by (I know it's not true)
> performing OR 64 on each char I want "fghijkl" out.


??? The characters in "abcdefg" already have bit 7 set on - as the must
since ord('a') is 97 > 64 (at least in ASCII, and many derived encodings).
>
> Any idea's?


Learn how to use apostrophes correctly, for a start

> Code examples would be appreciated.


Here's some code that does what I think you want, but as I have
described above, that is not actually that clear. I bet that there are
nicer ways to do this too, which others will most likely soon point out

----

#!/usr/bin/perl
use strict;
use warnings;

while (my $line = <DATA>) {
chomp $line;
my @line_array = split //, $line;
my @new_line_array = map {$_ | chr(64)} @line_array;
my $new_line = join '', @new_line_array;
print "$new_line\n";
}


__DATA__
abcdefg
1234567687568
%^&*^*()&^)&^

----

Output:

abcdefg
qrstuvwvxwuvx
e^fj^jhif^if^


DS
 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      06-23-2006
David Squire wrote:
> (E-Mail Removed) wrote:
>> Hi everyone
>>
>> I'm pretty crap at perl, so I'd appreciate so help from you guys.
>>
>> I have a string value held in $body variable.
>>
>> What I need to do is manipulate each individual character value in the
>> string with OR - "|" and then replace that character with the
>> character's new value.
>>
>> I'm using chr(ord($c) | 64) to get the new value, but I'm stuck on two
>> things: -
>>
>> 1) How to go through the string byte by byte and perform the OR 64 on
>> it
>> 2) How to get the character equivalent back into the string in the
>> right place


[snip]

> Here's some code that does what I think you want, but as I have
> described above, that is not actually that clear. I bet that there are
> nicer ways to do this too, which others will most likely soon point out


[snip]

.... such as this, which explicitly deals with bytes, rather than hoping
that that is what characters are in the default encoding:

----

#!/usr/bin/perl
use strict;
use warnings;

while (my $line = <DATA>) {
chomp $line;
my @line_array = unpack 'C*', $line;
my @new_line_array = map {$_ | 64} @line_array;
my $new_line = pack 'C*', @new_line_array;
print "$new_line\n";
}

----

DS
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      06-23-2006
David Squire <(E-Mail Removed)> writes:

> ??? The characters in "abcdefg" already have bit 7 set on - as the
> must since ord('a') is 97 > 64


??? The value of a bit is 2^position, starting at position 0.

2^7 = 128.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      06-23-2006
Sherm Pendley wrote:
> David Squire <(E-Mail Removed)> writes:
>
>> ??? The characters in "abcdefg" already have bit 7 set on - as the
>> must since ord('a') is 97 > 64

>
> ??? The value of a bit is 2^position, starting at position 0.
>
> 2^7 = 128.


I started counting at 1. The OP stated that he was doing | 64, so the
bit reffered to was clear in any case.

DS
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      06-23-2006
David Squire <(E-Mail Removed)> writes:

> Sherm Pendley wrote:
>> David Squire <(E-Mail Removed)> writes:
>>
>>> ??? The characters in "abcdefg" already have bit 7 set on - as the
>>> must since ord('a') is 97 > 64

>> ??? The value of a bit is 2^position, starting at position 0.
>> 2^7 = 128.

>
> I started counting at 1.


Yes, obviously - that's why I posted the correction. Beginning at one is
incorrect in any base-n notation, not just binary. For any value of n, the
value of position x as n^x. That only works when the positions are numbered
starting with zero.

It's not a matter of personal preference or opinion, it's part of the math-
ematical definition of base-n notation.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      06-23-2006
Sherm Pendley wrote:
> David Squire <(E-Mail Removed)> writes:
>
>> Sherm Pendley wrote:
>>> David Squire <(E-Mail Removed)> writes:
>>>
>>>> ??? The characters in "abcdefg" already have bit 7 set on - as the
>>>> must since ord('a') is 97 > 64
>>> ??? The value of a bit is 2^position, starting at position 0.
>>> 2^7 = 128.

>> I started counting at 1.

>
> Yes, obviously - that's why I posted the correction. Beginning at one is
> incorrect in any base-n notation, not just binary. For any value of n, the
> value of position x as n^x. That only works when the positions are numbered
> starting with zero.
>
> It's not a matter of personal preference or opinion, it's part of the math-
> ematical definition of base-n notation.


And entirely unrelated to helping with the OP's question. I can just as
easily say that the value at the nth position is x^(n-1), and then count
1st, 2nd, 3rd, etc.

You have again snipped context that made it clear that there was no
ambiguity in what I posted.

Choosing to start at 0 is indeed arbitrary - though of course you are
right about the most common convention.


DS
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      06-23-2006
David Squire <(E-Mail Removed)> writes:

> And entirely unrelated to helping with the OP's question.


Sorry. I guess I didn't realize I was getting paid for working at this
help desk and therefore obligated to answer questions.

> I can just
> as easily say that the value at the nth position is x^(n-1), and then
> count 1st, 2nd, 3rd, etc.


The difference is that I'm talking about an established rule that's been
widely agreed upon for decades - and that's just within the realm of
computer science. You, on the other hand, are just making stuff up to
rationalize your mistakes.

> You have again snipped context that made it clear that there was no
> ambiguity in what I posted.


You're right - It was unambiguously wrong.

> Choosing to start at 0 is indeed arbitrary


arbitrary, adj:
1. Determined by chance, whim, or impulse, and not by necessity, reason,
or principle: stopped at the first motel we passed, an arbitrary
choice.
2. Based on or subject to individual judgment or preference: The diet
imposes overall calorie limits, but daily menus are arbitrary.
3. Established by a court or judge rather than by a specific law or
statute: an arbitrary penalty.
4. Not limited by law; despotic: the arbitrary rule of a dictator.

The original decision to start at zero was indeed arbitrary. But that was a
long time ago. One could just as easily argue that the use of the Arabic
numerals 1 and 0 are arbitrary.

Now it's an established convention, and following it is not subject to
individual judgment or preference, assuming of course that you expect to
be understood.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-23-2006
(E-Mail Removed) wrote:
> Hi everyone
>
> I'm pretty crap at perl, so I'd appreciate so help from you guys.
>
> I have a string value held in $body variable.
>
> What I need to do is manipulate each individual character value in the
> string with OR - "|" and then replace that character with the
> character's new value.
>
> I'm using chr(ord($c) | 64) to get the new value
> [...]


Then you're pretty much there. Just use the substitution operator to
replace each character with the result of the code you have above, and
you're almost set.

You'll also have to change $c to the match variable $&, and the
substitution operator will need the 'g' option (global--go through the
entire string) and the 'e' option (execute code).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
Binary or Ascii Text? Claude Yih C Programming 31 04-17-2006 02:21 AM
ascii binary string into an integer sathyashrayan C Programming 5 04-14-2005 11:59 AM
Question about reading a big binary file and write it into several text (ascii) files Albert Tu Python 2 01-25-2005 03:56 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments