Displaying hex numbers as decimal?

Discussion in 'Perl Misc' started by faren451@gmail.com, Oct 10, 2007.

  1. Guest

    Hi, I have been trying to figure out how to display hex numbers as
    decimals after reading in the hex from a file. Reading several posts
    online, it seems like the easy way is to use printf. However, printf
    does not seem to like me using a variable to make the hex number. For
    example, my input files only have a 2-digit hex number, so I need to
    add 0x in front to make printf work (as I found by experimenting with
    the command line), but this does not work well with variables--
    everything is converted to 0's. My script is below:

    print "Enter filename to parse: ";
    chomp($filename = <>);
    $FilePath = "$filename";
    sysopen(HANDLE, $FilePath, O_RDONLY) or die "Cannot open file";
    @events = <HANDLE>;
    close(HANDLE);
    $Outfile = "out_$filename";
    sysopen(OHANDLE, $Outfile, O_RDWR|O_CREAT, 0755) or die "Cannot create
    file";
    foreach $events (@events)
    {
    $fieldnum = 0;
    while($events =~ /(\s.*?\s)/g)
    {
    $fieldnum++;

    # date/time/week
    if($fieldnum == 2)
    {
    printf OHANDLE "$1,";
    }

    # hex data fields
    if($fieldnum > 6)
    {
    $tmp = $1;
    $tmp =~ s/^\s+//;
    $tmp =~ s/\s+$//;
    $hexval = "0x$tmp";
    printf OHANDLE "$hexval=>%d,", $hexval;
    printf "$hexval=>%d,", $hexval;
    }
    }
    close(OHANDLE);


    Sample data fields in the file are:


    EB 70 07 8A 51 C5 98 1B 01 00 00 00 00 00 00 02 00 00
    00 00 00 00 00 00 00 01 01 00 01 00 00 00 00 00 00
    00 02 00 00 00 01 00 01 01 00 01 02 00 00 00 00 00
    01 00 01 00 00 93 10 60 00



    I appreciate any help! I am a completely Perl newbie, so I have no
    idea why this will not work. The built-in hex() function also does
    not work, I suspect for the same reason?

    Thanks!
    , Oct 10, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    wrote:
    > Hi, I have been trying to figure out how to display hex numbers as
    > decimals after reading in the hex from a file. Reading several posts
    > online, it seems like the easy way is to use printf. However, printf
    > does not seem to like me using a variable to make the hex number. For
    > example, my input files only have a 2-digit hex number, so I need to
    > add 0x in front to make printf work (as I found by experimenting with
    > the command line), but this does not work well with variables--
    > everything is converted to 0's. My script is below:
    > ...
    > I appreciate any help! I am a completely Perl newbie, so I have no
    > idea why this will not work. The built-in hex() function also does
    > not work, I suspect for the same reason?


    You have to differentiate between 'numeric literals', like 0xabc
    or 123456 or 0333 and the actual perl values, which are of type
    "signed integer" (IV) or "unsigned integer" (UV).

    If you write them out, you choose a "representation", which might
    be "binary" (%b) or "decimal" (%d) or hex (%h). The Perl function
    "hex" does convert *strings* containing a hex sequence
    (eg. perl -e ' print hex "10" ' ==> prints 16)

    If you have hex-strings in your file, then you just
    put them in the hex function and you'll get "normal
    perl values" out, which might be written in any
    representation later on.
    Example:
    ==>
    ...
    print "Enter filename to parse: ";
    chomp(my $filename = <>);
    my $FilePath = $filename;

    open my $fh, '<', $FilePath or die "$FilePath - $!";
    my @events = <$fh>;
    close $fh;

    my $Outfile = "out_$filename";
    open $fh, '>', $Outfile or die "$Outfile - $!";

    for my $events (@events) {
    my $fieldnum = 0;
    while( $events =~ /(\S+)/g ) {
    # date/time/week
    printf $fh "$1," if ++$fieldnum == 2;
    # hex data fields
    if($fieldnum > 6) {
    my $hexval = hex $1;
    printf $fh "%02X=>%d,", $hexval, $hexval;
    printf "%02X=>%d,", $hexval, $hexval
    }
    }
    }
    close $fh;
    ...
    <==

    Regards

    Mirco
    Mirco Wahab, Oct 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,110
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    439
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    566
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,766
  5. Todd Gardner
    Replies:
    2
    Views:
    128
    Todd Gardner
    Jun 21, 2004
Loading...

Share This Page