CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
writing hex values to a file

 



hestes37
Novice

May 20, 2010, 1:40 PM

Post #1 of 12 (1438 views)
writing hex values to a file Can't Post

Hi folks,

I'm very new to PERL, been using it a few weeks. I'm trying to append 4 integers stored in an array to a flat file and I keep getting for example using 1,2,3,4, I get 31 32 33 34. I'm trying to get 1, 2 , 3, 4.

code snippet:
foreach $hexnum (@versionnum ){

sprintf "%lx", $hexnum;
print FILE $nulchar;
print FILE $hexnum;

}


I use the $nulchar to append 0x00 to the byte before each integer I want to convert so I'm writing 8 bytes to the end of the file.

Any help I could get with this would be great. Been looking for about a week and still haven't found it.

H


BillKSmith
Veteran

May 20, 2010, 7:10 PM

Post #2 of 12 (1430 views)
Re: [hestes37] writing hex values to a file [In reply to] Can't Post

I am not sure I understand your problem. I assume that @versionnumber is an array of four integers, each in the range 0..15. Your output consists of a null character and the hex representation (0..F) for each of the integers.

Your call of sprintf function does not do anything useful. Your lx format specifier is seldom correct in perl. I believe that the documentation of formats assumes that you are already familiar with C. You probably can get a better description from C-language documents.

I find formated I/O clearer if the fixed characters are in the format rather than arguments.


Code
  

use strict;
use warnings;
use 5.8.8;
use charnames qw( :full );
my @version_num = qw( 1 11 3 4 );

open my $FILE, '>>', "somefile.hex" or die "unable to open version list file\n";
printf $FILE "\N{NULL}%1.1X"x4, @version_num;
close $FILE;



This code appends 8 bytes "*1*B*3*4" to the end of somefile.hex.
Good Luck,
Bill


hestes37
Novice

May 21, 2010, 2:08 PM

Post #3 of 12 (1422 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Thanks Bill for the suggestion. This seems like a cleaner solution. I'll try it Monday. I'll also review the c printf reference.

H


hestes37
Novice

May 24, 2010, 1:57 PM

Post #4 of 12 (1399 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Bill,
I'm using version 5.8.4 not 5.8.8. don't know if that makes a difference. Also, I have a little subroutine that grabs the 4 digits from the command line that the user inputs. Then I do some error checking on the numbers to make sure they're 4 digits. Then I use a 'split' function to move the numbers from one variable to the array.

($version contains the numbers the user inputs)

my @versionNums = split(undef, $version);

so to integrate you're suggestion I took the above out and wrote
my @versionNums = qw($version);
.
.
.
then
print FILE, "\N{NULL}%1.1Xx4, @version_num;

.
.
.

I'm quite sure I'm not doing something right because the file will get 4 zero's written to it. I've been scouring the net looking for some examples but couldn't find any printf examples that contain "\N{NULL} in the beginning. The formatting following that is clear, except for the x4 part.

Does it matter that I'm using 5.8.4?
Thanks,
H


BillKSmith
Veteran

May 24, 2010, 4:44 PM

Post #5 of 12 (1394 views)
Re: [hestes37] writing hex values to a file [In reply to] Can't Post

Easy things first. Let me try to explain the parts of my solution that you did not understand.



I was lazy with the use version. It only has to be new enough (5.6) for "use charnames" to work. That module provides the magic for \N{NULL}. This is an escape sequence for one NULL character. It is exactly the same as \000, but easier to read. Learn more than you care to know from perldoc charnames.



'x" is the perl repetition operator. It tells perl to repeat the previous string the number of times specified by the following argument (in this case, 4). Refer to perldoc perlop and search for 'repetition'.

The string is the format that tells printf to output one null character and exactly one hex character which is specified by an integer argument. This pattern could have been repeated four times, but the x4 saved typing and counting errors.



I used @versionNums = qw(.... to simulate my understanding of your print problem. I expected you to tell my about details I had wrong. I think that I was correct, but it seems I misled you.

What exactly does $version contain? If it contains a string of four digits, Your original split should work. I am not sure about the undef. The documentation for split specifies that the first argument should be a pattern. I would specify a null string .....split( //, $version ). It probably would be wise to print $version while you are debugging.
Good Luck,
Bill


hestes37
Novice

May 24, 2010, 6:11 PM

Post #6 of 12 (1392 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Thanks for the breakdown Bill. I'll research the points you mentioned in the reply. $version does contain a string of 4 numbers and when I use print debug statements I get for testing 1 2 3 4.

Does it matter that i've opened the file already, truncated it to the current file size - 8 bytes, (since I want exactly a certain file size after I've added the 8 bytes) then tried to append to the already opened file with your code, then close the file? After truncating, I don't have to close then reopen the file do I.
H


BillKSmith
Veteran

May 24, 2010, 9:06 PM

Post #7 of 12 (1388 views)
Re: [hestes37] writing hex values to a file [In reply to] Can't Post

Lets just work on creating @versionNums from $version.

You show spaces between the digits in $version. Is this for real? What happens if one or more of the fields becomes greater than nine? Does the number become '10' or 'A'? Are the fields always exactly one character? Two? Variable? You cannot parse this variable correctly without a complete accurate specification of its contents. If the contents do not meet spec, the program should die rather than corrupt your data file.



The questions you are asking about I/O are getting off topic. Try to write a very simple, but complete, program which does nothing but truncates your file. Verify that this works for several different files. If it does, modify the program to append eight printable characters to the truncated file. Verify that this works. If it does, you have answered your own questions. If you still need help, post a specification of 'truncate', your complete truncation program, and a realistic sample data file. Explain what it goes wrong.
Good Luck,
Bill


hestes37
Novice

May 25, 2010, 5:05 PM

Post #8 of 12 (1380 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Bill,

I haven't included the parts of this program that do that. Would you like me to just test them myself or post so that you can see what I'm doing?

h


BillKSmith
Veteran

May 26, 2010, 1:52 PM

Post #9 of 12 (1372 views)
Re: [hestes37] writing hex values to a file [In reply to] Can't Post

I was tired and rather curt in my last reply. It seems you have two related problems. You must correctly format the version data and write it to the right place. I do not have enough info to help with the second.

After re-reading the entire thread, I no longer believe that the actual version data will ever be in hex. Your original split correctly splits the data into four decimal (not hex) digits. Therefore, it would be better (although not necessary) to change my %1.1x format specifier to %1.1u. Clearly the second data field in my example should be changed to a single decimal digit.

Returning to the second issue. I am not sure exactly what you mean by 'truncate' the file. It sounds like you want to overwrite the last eight bytes of an existing file. Refer to the perl seek function. (perldoc -f seek) Don't ask for help until you have given it a good try. I recommend that you back-up your original data file before attempting to overwrite part of it.

open file for output

seek to right place

printf the new data.

good luck
Good Luck,
Bill


hestes37
Novice

May 26, 2010, 5:53 PM

Post #10 of 12 (1368 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Thanks Bill,

I'll give it a shot.

H


hestes37
Novice

May 27, 2010, 4:25 PM

Post #11 of 12 (1354 views)
Re: [BillKSmith] writing hex values to a file [In reply to] Can't Post

Hi Bill,



Still working on your suggested program. Wanted to mention though I found this surfing around the net after trying to use SEEK() on opening a file to append. http://davesource.com/Bugs/perl.7.html. Aparently there is some kind of bug with SEEK() only when appending to a file? Since I'm new at this, I'm not sure how old/new this discovery is but I'm going to use the truncate() function to remove the last 8 bytes from my file.



H


BillKSmith
Veteran

May 28, 2010, 6:24 AM

Post #12 of 12 (1351 views)
Re: [hestes37] writing hex values to a file [In reply to] Can't Post

It sounds like you know more than I about this subject. Your reference made one point that you may have missed. It says that append mode takes precedence over seek. The truncate function may have the same problem. Open the file for output (not append) when using these functions.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives