Alright so I'm reading in line by line from a file. The lines from the input file can either be 2, 4, or 6 characters long. If they are 2 digits long I'm to add (append) 4 zeros to the end of the line, and if the line is 4 digits long I'm to add 2 0's, and if they're 6 they're good to go. I'm having a breakdown trying to figure out how to add these digits to the magic variable without a space to the recognized by the next part of my program (which coverts the 6 digits into Base64). Here's my code:

Re: [cupofdoug] Appending to the end of a line from an input file
[In reply to]

Can't Post

Personally, if I had to solve the overall problem, I would probably use rather something like Kenosis or Fishmonger have suggested, but I tried in my answer to give a solution to the exact problem you were asking and with the minimal amount of changes compared to your code.

I was given the "my $binary = sprintf("%08b", $asciiChar);" part, and I'm not sure of how it works to convert an ascii character to binary, and now I need to convert binary back to ascii (and then ascii back to plaintext, I'm assuming using the "chr" function). So basically I have to reverse this process. Thanks again for any suggestions.

Re: [cupofdoug] Appending to the end of a line from an input file
[In reply to]

Can't Post

Hi,

I think that:

Code

my $binary = sprintf("%08b", $asciiChar);

is probably not doing what you think. In this case, the spintf converts your number not into a binary number, but into a string containing a binary representation of your number, i.e. a string o characters containing only the 0 and 1 characters.

For example, if you do this:

Code

$binary = sprintf("%08b", 17);

$binary now contains the following string of characters: "00010001", which is probably not what you are looking for. So, you probably don't want to do that conversion, and I am not sure you need to do any conversion, since the ascii value returned by the ord function is a number.

Re: [Laurent_R] Appending to the end of a line from an input file
[In reply to]

Can't Post

A string representation of the binary actually is exactly what I needed for that situation (still don't know exactly how that sprintf works unfortunately). The project I'm working on calls for a provided text file to be encoded into Base64 formatting from plain text. The process I was told to follow involves first converting each character of the plaintext to its ASCII representation, then converting the ASCII representation to its binary representation, then reading in chunks of 6 characters at a time from the binary string and converting these 6 char chunks into their Base64 equivalent (not always evenly distributed into 6 bit chunks, which is why I needed to append 00's to the end of the string on occassion). Finally I was to print the Base64 "code" into a new file.

The next part of the project calls for a text file already encoded into Base64 to be decoded into plaintext, so I'm reversing the process. I can get the binary representation from the Base64 character using a pretty lengthy if/elsif statement, but converting the binary representation to its ASCII equivalent is where I'm stuck.

Re: [cupofdoug] Appending to the end of a line from an input file
[In reply to]

Can't Post

For coding in base64 use this CPAN module:

Code

use MIME::Base64;

For example, you could encode a whole file in base64 using the following one-liner:

Code

perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' your_file.txt

As for the sprintf function, when you do:

Code

$binary = sprintf("%08b", 17);

The first argument is a format string in which: - % says that you are applying the format to the next argument (17 in this case) - 08 says that you want the resulting string to be formated on 8 characters padded with 0 on the lext - d that the next argument should be interpreted as a signed integer, in decimal.

For more information, see: http://perldoc.perl.org/functions/sprintf.html

Re: [cupofdoug] Appending to the end of a line from an input file
[In reply to]

Can't Post

In Reply To

I can get the binary representation from the Base64 character using a pretty lengthy if/elsif statement, but converting the binary representation to its ASCII equivalent is where I'm stuck.

Use the oct function after having prepended "0b" to your binary string.

Quote from perldoc:

Code

perldoc -d oct

oct EXPR oct Interprets EXPR as an octal string and returns the corresponding value. (If EXPR happens to start off with "0x", interprets it as a hex string. If EXPR starts off with "0b", it is interpreted as a binary string. Leading whitespace is ignored in all three cases.)