Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting ASCII values 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
0
0
GB
I am trying to get the ascii equivalent of some items within a flat file. The file is a .jpg picture but the file type isn't important to this problem.
I have the following code.
Code:
open (LOG, "<".$LARGE_PICTURE) || &errormess;
read LOG, $letw, 1, 2;
read LOG, $leth, 1, 3;
close (LOG);
$w=ord($letw);
$h=ord($leth);
print "ed*$letw* - $w <br> *$leth* - $h<br>";
This code returns the following.
Code:
*ÿ* - 0 
*Ø* - 0
$letw and $leth have chr values but I am unable to get the acsii equivalent.
Could someone explain why the values of $w and $h are 0 and not the Ascii value of $letw and $leth.

Keith
 
From perldoc read
read FILEHANDLE,SCALAR,LENGTH,OFFSET
read FILEHANDLE,SCALAR,LENGTH
Attempts to read LENGTH *characters* of data into variable
SCALAR from the specified FILEHANDLE. Returns the number of
characters actually read, 0 at end of file, or undef if there
was an error. SCALAR will be grown or shrunk to the length
actually read. If SCALAR needs growing, the new bytes will be
zero bytes. An OFFSET may be specified to place the read data
into some other place in SCALAR than the beginning.
The call is
actually implemented in terms of either Perl's or system's
fread() call. To get a true read(2) system call, see "sysread".

Note the *characters*: depending on the status of the
filehandle, either (8-bit) bytes or characters are read. By
default all filehandles operate on bytes, but for example if the
filehandle has been opened with the ":utf8" I/O layer (see
"open", and the "open" pragma, open), the I/O will operate on
characters, not bytes.
By supplying an offset in your read statements, you're putting the result of your first read at an offset of 2 from the beginning of $letw and the result of your second read at an offset of 3 from the beginning of $leth. (This explains why $leth shows a length of 4.) I don't know what you were trying to accomplish with the offsets, but I'm sure the above was not what you intended. If you drop the offsets you should read one character into $letw and one character into $leth. ord(), which returns the ascii value of a single character, should then work properly.



 
Thanks for the replies guys.
I can't believe that PERL makes this function difficult.
eg. FOXPRO -
Code:
leth=asc(substr(source_string,2,1))

CGILOVER
Where does 'C*' come from?

MIKEVH
I assumed the offset referred to the source string rather than the target. I need to get the ascii values of chrs 2,3,4 and 5 from the source file. I am only working on 2 chrs until I manage to work it out. As I see it, dropping the offests will give me the same value in both vars, not what I intended either.
Code:
read LOG, $letw, 1;
read LOG, $leth, 1;


Keith
 
<i>"I can't believe that PERL makes this function difficult."</i>
<b>it's not!</b>

Here's another way.

Code:
# convert from number to letter
# using chr()
my $number =  chr(65);   # A

# convert from letter to number
# using ord()
my $letter =  ord(A);    # 65
 
My confusion has been compounded by the read section rather than getting the ascii value.
ord is exactly the same as the more familliar, to me, asc.
Thanks guys, why is it always the simple stuff that slows me down.
I'm back on track now and that section is already finished.
Thanks once again for getting my project back on track.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top