🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.java
Article #153925

Re: obliczenie sumy kontrolnej (w C+ działa) a w java?

#153925
From: bigMamma
Date: Sun, 20 Oct 2013 17:35
90 lines
2975 bytes
W dniu sobota, 19 października 2013 20:05:35 UTC+2 użytkownik Arivald napisał:
> W dniu 2013-10-19 19:21, bigMamma pisze:
> 
> > Dzień dobry,
> 
> > mam następujący problem.
> 
> > Oryginalny kod w C+:
> 
> >
> 
> > short CalcCrc(char *str,int len)
> 
> > {
> 
> > short crc=0;
> 
> > for(int i=0;i<len;i++)
> 
> > crc ^= (short)((unsigned char)str[i]) << (i%9);
> 
> > return crc;
> 
> > }
> 
> >
> 
> > wylicza sumę kontrolną. Przepisałem to w Java:
> 
> >
> 
> > public static byte [] CRC(byte[] dane) {
> 
> >
> 
> >          short crc = 0;
> 
> >          for (int i = 0; i < dane.length; i++) {
> 
> >              crc ^= (short)(( char)dane[i]) << (i%9);
> 
> >          }
> 
> > String  w = String.format("%04X", crc);
> 
> > return w.getBytes();
> 
> >     }
> 
> >
> 
> > niestety, nie zwraca mi prawidłowej sumy kontrolnej. Czy mogę prosić o pomoc?
> 
> > ps. operacja na CRC to ma być XOR z 16 bitową liczbą, przsuniętą w lewo o modulo z 9 do swojej pozycji.
> 
> >
> 
> 
> 
> 
> 
> "char" w javie jest 2 bajtowy. użyj zamiast tego "byte"
> 
> 
> 
> Po za tym dane wejściowe są już "byte", rzut niepotrzebny. W C jest 
> 
> rzut, dla uzyskania "unsigned", bo "char" w C jest "signed".
> 
> 
> 
> 
> 
> -- 
> 
> Arivald

Dziękuję serdecznie!
Rzeczywiście, teraz działa!
public static byte [] CRC(byte[] dane) {

        short crc = 0;
        for (int i = 0; i < dane.length; i++) {
           
            crc ^= (short)((byte)dane[i]) << (i % 9);
            
        }
        String  w = String.format("%04X", crc);
      return w.getBytes();
    }
Technical Details
Message-ID: <6fe1e1d0-e7d1-4211-8e21-4a644d22c3ad@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin1!goblin.stu.neva.ru!i2no23471987qav.0!news-out.google.com!9ni58915qaf.0!nntp.google.com!i2no23471977qav.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
References: <ac680459-12a5-4824-9c86-55e44cd487c3@googlegroups.com> <l3uhle$hni$1@news.dialog.net.pl>

Show Full Headers
Complete Article Headers
X-Received: by 10.182.247.102 with SMTP id yd6mr5028099obc.39.1382290523586;        Sun, 20 Oct 2013 10:35:23 -0700 (PDT)
X-Received: by 10.49.48.38 with SMTP id i6mr361316qen.4.1382290523559; Sun, 20 Oct 2013 10:35:23 -0700 (PDT)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin1!goblin.stu.neva.ru!i2no23471987qav.0!news-out.google.com!9ni58915qaf.0!nntp.google.com!i2no23471977qav.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.java
Date: Sun, 20 Oct 2013 10:35:23 -0700 (PDT)
In-Reply-To: <l3uhle$hni$1@news.dialog.net.pl>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=188.122.6.105; posting-account=S68uugoAAADAKFnRPdRLP91xa88xg3LE
NNTP-Posting-Host: 188.122.6.105
References: <ac680459-12a5-4824-9c86-55e44cd487c3@googlegroups.com> <l3uhle$hni$1@news.dialog.net.pl>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6fe1e1d0-e7d1-4211-8e21-4a644d22c3ad@googlegroups.com>
Subject: Re: obliczenie sumy kontrolnej (w C+ działa) a w
 java?
From: bigMamma <marcinchuc@gmail.com>
Injection-Date: Sun, 20 Oct 2013 17:35:23 +0000
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit