Solved

# BigInteger and byte array

Posted on 2003-03-13
Medium Priority
1,082 Views
How I can represent the following 8 bytes in a byte[] 0x00ff000000000000 ?
In other words, I want something like:
byte[] myBytes = { 00, ff, 00, 00, 00, 00, 00, 00 };
I know 0xff is an unsigned byte and I can use char as its type, but I want to construct a BigInteger with that array.
BigInteger bi = new BigInteger(myBytes);
Any suggestions?
0
Question by:s_lavie
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Expert Comment

ID: 8126722
>> BigInteger bi = new BigInteger(myBytes);

The above is ok. One of the constructors takes a byte array.

0

LVL 2

Author Comment

ID: 8126931
initiate that array when one of the entries is 0xff
0

LVL 9

Expert Comment

ID: 8126946
byte[] myBytes = { 0x00b, 0xffb, 0x00b, 0x00b, 0x00b, 0x00b, 0x00b, 0x00b };
0

LVL 16

Accepted Solution

Peter Kwan earned 200 total points
ID: 8126983
I have tried Venci75's comment, and still getting possible loss of precision compilation error.

I think one way is to explicit casting to byte:
byte[] myBytes =  { 0x00, (byte)0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
0

LVL 2

Author Comment

ID: 8127137
Does (byte)0xff can cause any harm?
0

LVL 9

Expert Comment

ID: 8127156
no - it simply gets the lowes byte from the integer value - which is exactly what you want
0

LVL 2

Author Comment

ID: 8127201
Thank you all
0

## Featured Post

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
###### Suggested Courses
Course of the Month12 days, 15 hours left to enroll