Re: binary conversion
K D <[email protected]>
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <[email protected]> |
thank you Erling, your's way and Niall's way, both work thank you very much.. Ken --- Erling Egholm <[email protected]> wrote: > Try this ... > > int num [] = new int[4]; > > num[0] = Integer.parseInt("00000000", 2); > num[1] = Integer.parseInt("00001110", 2); > num[2] = Integer.parseInt("00110101", 2); > num[3] = Integer.parseInt("10000001", 2); > > int bigE = ((num[3]) | ((num[2]) << 8) | > ((num[1]) << 16) | ((num[0]) << 24)); > > System.out.println("bigE = " + bigE); > > // Output: bigE = 931201 > > Erling > > ----- Original Message ----- > From: "K D" <[email protected]> > To: <[email protected]> > Sent: Friday, May 23, 2003 7:38 PM > Subject: Re: binary conversion > > > > Hi, > > > > I tried your method, and I got "0" for both way > > I am not sure why, > > but here is my input > > > > 00000000 > > 00001110 > > 00110101 > > 10000001 > > > > these number = 931201 (year,month,day) > > > > thanks > > > > > > --- Niall Gallagher <[email protected]> wrote: > > > >In the example that you gave it, > > > >you are converting an integer > > > >into big and small endian right? > > > > > > > >I guess what I tried to ask was > > > >I have a 4 byte of data, > > > >and it is not stored in ascci format > > > >but rather in byte data only, so I have > > > >to read in data as byte array like this... > > > > > > > >byte num [] = new byte[4] > > > > > > > > > Hi > > > > > > What you have is a list of 8-bit bytes and what > you > > > want to do > > > is to convert these into a single integer. So > for > > > example if > > > the integer was 1 you have the equivelant of > > > > > > a [0000 0001] > > > b [0000 0000] > > > c [0000 0000] > > > d [0000 0000] > > > > > > Where a is num[0] and d is num[3]. Well if I > > > remember big > > > endian right what you want is the binary 32-bit > > > integer > > > > > > [0000 0000 0000 0000 0000 0000 0000 0001] > > > > > > So to do this the following expression should > work. > > > > > > int bigE = ((num[0] & 0x00) > > > | ((num[1] & 0x00) << 8) > > > | ((num[2] & 0x00) << 16) > > > | ((num[3] & 0x00) << 24)); > > > > > > This should give you the correct integer value, > > > However > > > I may have my big and little endian upside down, > in > > > which > > > case the following sould work > > > > > > int bigE = ((num[3] & 0x00) > > > | ((num[2] & 0x00) << 8) > > > | ((num[1] & 0x00) << 16) > > > | ((num[0] & 0x00) << 24)); > > > > > > > > > I have had to do this many times before, so you > > > should > > > have no problem with these expressions, if you > do, > > > let me know. > > > > > > Niall > > > > > > > > > ___________________________________________________________________________ > > > To unsubscribe, send email to > [email protected] > > > and include in the body > > > of the message "signoff SERVLET-INTEREST". > > > > > > Archives: > > > > > > http://archives.java.sun.com/archives/servlet-interest.html > > > Resources: > > > > > > http://java.sun.com/products/servlet/external-resources.html > > > LISTSERV Help: > > > http://www.lsoft.com/manuals/user/user.html > > > > > > > > > __________________________________ > > Do you Yahoo!? > > The New Yahoo! Search - Faster. Easier. Bingo. > > http://search.yahoo.com > > > > > ___________________________________________________________________________ > > To unsubscribe, send email to > [email protected] and include in the body > > of the message "signoff SERVLET-INTEREST". > > > > Archives: > http://archives.java.sun.com/archives/servlet-interest.html > > Resources: > http://java.sun.com/products/servlet/external-resources.html > > LISTSERV Help: > http://www.lsoft.com/manuals/user/user.html > > > > ___________________________________________________________________________ > To unsubscribe, send email to [email protected] > and include in the body > of the message "signoff SERVLET-INTEREST". > > Archives: > http://archives.java.sun.com/archives/servlet-interest.html > Resources: > http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: > http://www.lsoft.com/manuals/user/user.html > __________________________________ Do you Yahoo!? Yahoo! Calendar - Free online calendar with sync to Outlook(TM). http://calendar.yahoo.com ___________________________________________________________________________ To unsubscribe, send email to [email protected] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html