Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to read "long" in to byte table

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I'm making a small application for J2ME. I need a very random seed for a certain key. For that seed I have created a very long long Those long's can be like this 3460215174411169804

How can I read this long to this kind of byte-table:



Remember, I'm using J2ME, so I don't have SecureRandom-class in use, which could be used with Java SE for this kind of purposes to make random byte-tables.

Br
Koot
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You basically want to convert long to byte[]?

Then just do that You know, long is 64bits and each byte can hold 8bits. You need to shift 8bits from the long 8 times and get each of them in the byte array.

Alternatively you can also use ByteArrayOutputStream wrapped by a DataOutputStream to write long to it and to get a byte array from it. Not sure if it is available in J2ME.

Alter-alternatively you can also construct a BigInteger with the String representation of the Long and get a byte array from it. Also not sure if it is available in J2ME.
 
Koot Jart
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bauke and thanks for your answer!

Bauke Scholtz wrote:You basically want to convert long to byte[]?

Then just do that You know, long is 64bits and each byte can hold 8bits. You need to shift 8bits from the long 8 times and get each of them in the byte array.



Well, basically this is what I need. But how to do it in practice? Do you have or know good examples of this? I'm not so familiar with byte-operations.

Those other alternatives are good options, but I guess they are not available in J2ME.



 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, basically this is what I need. But how to do it in practice? Do you have or know good examples of this? I'm not so familiar with byte-operations.



Well, the lowest order byte is....

byte loByte = (byte) (longvar & 0xff);


And the next higher byte is ....

byte nextByte = (byte) ((longvar >> 8) & 0xff)


And... the rest you can figure out. BTW, you should get up to speed on the bit shifters and operators.

Henry
 
Koot Jart
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry and others!

I have made a piece of code:



I don't really know if it is working properly or not. Could you now help me, how to convert this byte-table back to long. Then I can see, that everything is all right when converting long to byte originally.

It's funny how hard it seems to be, to find any examples of this.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't really know if it is working properly or not.



Maybe if you started with something like this....



You can then print the result and see if the bytes are correct?? But to answer the question... No. It's not working properly.

Henry
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's funny how hard it seems to be, to find any examples of this.



The bitwise (and logical) operators, along with the shifting operators are straightforward. I would really recommend taking a few minutes to learn them -- instead of googling for examples on this particular issue.

In the time that you posted this question, and discussed it, you could have learned the operators, and solved the problem yourself, many times already.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be a tricky issue. Maybe later some more.
reply
    Bookmark Topic Watch Topic
  • New Topic