Solved

Converting long to bytes

Posted on 1998-10-12
11
260 Views
Last Modified: 2008-02-01
Having trouble as CRC16 returns a long and I want it to go into an array of two bytes. How do you convert int or long numbers into bytes. Otherwise, does anyone know where to get a 16 bit CRC class that does this.
0
Comment
Question by:yung
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1225680
Hi 'yung',

Try this out. It converts the 64-bit long into 8 bytes and puts it into an array. You can apply it also to 32-bit ints.


            byte[] barray = new byte[8];
            long L = 0x0A000B000C000D00L;
            long byte_mask = 255;

            for (int i = 7; i >= 0; i--)
            {
              barray[i] = (byte)((L & (byte_mask << (8 * i))) >> (8 * i));      
              System.out.println(barray[i]);
            }



0
 

Author Comment

by:yung
ID: 1225681
Sorry if this is a dumb question, but what does " 0x0A000B000C000D00L" mean, is this hex? If I'm using the code for an int what would I use?

Ta
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225682
Yes, I used hex just as an example. There you clearly see the bytes. But you can use plain numbers as well. Like
L = 12341235424312L;
In case of int:

byte[] barray = new byte[4];
int iii = 0x0A000D00L; //or something like 341234;
int byte_mask = 255;

for (int i = 3; i >= 0; i--)
{
  barray[i] = (byte)((iii & (byte_mask << (8 * i))) >> (8 * i));
  System.out.println(barray[i]);
}
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 6

Expert Comment

by:gadio
ID: 1225683
yung, here is another way of doing that (I prefer it its a bit more elegant):



public static byte[] longToByteArray(long num) {
   ByteArrayOutputStream baos;
   try {
      DataOutputStream dos = new DataOutputStream(baos = new ByteArrayOutputStream());
      dos.writeLong(num);
      return baos.toByteArray();
   } catch( Exception e ) {}
   return null;
}
           
0
 

Author Comment

by:yung
ID: 1225684
Thanks...both do the trick. I'll finally finish this datacomms assignment. You'll need to post as answrs tho, or else I can't unload the points.

Thanks
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225685
The 'gadio's answer is more elegant and Java style indeed. However, I still prefer the bitwise operations for small values like 1 long.

Say who takes the points.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1225686
yung, wherever the point are going to, learn this pattern of conversion in java, you can use it to change any type to bytes and back this way, even when you don't have a bitwize operations to do that (like doubles and floats).
0
 
LVL 1

Expert Comment

by:ramsay
ID: 1225687
Should I toss a coin?

Heads for Diakov, tails for gadio...
Ok.. here goes

woops.. where'd that coin bounce!!??
DOH!

:)
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225688
as far as I'm concerned points go to 'gadio'.
0
 
LVL 6

Accepted Solution

by:
gadio earned 50 total points
ID: 1225689
Thank you diakov.
0
 

Author Comment

by:yung
ID: 1225690
Hey, both answers were fine and both worked... and gadio, I have studied up on my bitwize operations. Maybe I'll even do well in my exams. Thanks to you both.

I'm giving the points only to gadio because diakov didn't actually post as an answer so I don't know how to do it any other way.
Cheers

yung
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 67
VB Script to add site to Java Exception List 4 78
Java Options in Zimbra Server 2 54
jsp login check 12 32
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question