Uhrzeit aus einem 2 Byte Hexcode ermitteln

Begonnen von CoolTux, 31 Januar 2018, 09:08:00

Vorheriges Thema - Nächstes Thema

CoolTux

Da ich mich die letzten Tage damit etwas, sagen wir, gequält habe halte ich es mal für die Nachwelt fest.

Gegeben ist der Hex String
Zeiten sind in Minuten *10

                                 6c 8f 00 33 00 00 36 37
                                 |  |  |  |  |  |  |  |
                                 |  |  |  |  |  |  |  end 4 - 0x37 = 55, 55 : 6 = 09:10
                                 |  |  |  |  |  |  start 4  - 0x36 = 54, 54 : 6 = 09:00
                                 |  |  |  |  |  end 3 - 0:00
                                 |  |  |  |  start 3  - 0:00
                                 |  |  |  end 2 - 0x33 = 51, 51 : 6 = 08:30
                                 |  |  start 2  - 0x00 =  0,  0 : 6 = 00:00
                                 |  end 1 - 0x8f = 143, 143 : 6 = 23:50
                                 start 1  - 0x6c = 108, 108 : 6 = 18:00


Daraus soll ein String bestehend aus insgesamt 8 Uhrzeiten erstellt werden. Jeweils eine Startzeit gefolgt von einer Endzeit.


my $output;
my $notification = '6c 8f 00 33 00 00 36 37';
my @tempList  = split(" ",$notification);
    my $i = 0;
    my $hour;
    my $min;

    foreach(@tempList) {
        ### Berechnung Stunden
        if( hex("0x".$tempList[$i])*10/6 < 100 ) {
            $hour = '0'.substr(hex("0x".$tempList[$i])*10/6,0,1);
        } elsif( hex("0x".$tempList[$i])*10/6 > 99 ) {
            $hour = substr(hex("0x".$tempList[$i])*10/6,0,2);
        }

        ### Berechnung Minuten
        if( hex("0x".$tempList[$i])*10/6 == 0 or (hex("0x".$tempList[$i])*10/6) =~ /^[1-9]0$/ ) {
            $min = '00';
        } elsif( int(hex("0x".$tempList[$i])*10/6) == hex("0x".$tempList[$i])*10/6 and hex("0x".$tempList[$i])*10/6 > 0 and hex("0x".$tempList[$i])*10/6 < 100 ) {
            $min = int(substr(hex("0x".$tempList[$i])*10/6,1)/10*60+0.5);
        } elsif( int(hex("0x".$tempList[$i])*10/6) == hex("0x".$tempList[$i])*10/6 ) {
            $min = int(substr(hex("0x".$tempList[$i])*10/6,2)/10*60+0.5).'0';
        } elsif( int(hex("0x".$tempList[$i])*10/6) != hex("0x".$tempList[$i])*10/6 and hex("0x".$tempList[$i])*10/6 > 99 ) {
            $min = int(substr(hex("0x".$tempList[$i])*10/6,2)/10*60+0.5);
        } elsif( int(hex("0x".$tempList[$i])*10/6) != hex("0x".$tempList[$i])*10/6 and hex("0x".$tempList[$i])*10/6 < 100 ) {
            $min = int(substr(hex("0x".$tempList[$i])*10/6,1)/10*60+0.5);
        }

        $output = $hour.':'.$min if($i == 0);
        $output = $output . ' ' . $hour.':'.$min if($i > 0);

        $i++;
    }

printf "\n" . $output . "\n";




Viel Spaß
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net