Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-22-2008, 06:15 AM
Member
 
Join Date: May 2008
Posts: 27
nanaji is on a distinguished road
Send a message via MSN to nanaji Send a message via Yahoo to nanaji
how to convert from BigInteger to Hex
Hi guys

I know we can convert from Integer to Hex by using
int i =2;
String s = Integer.toHexString(i);

Like wise

How can i convert BigInteger bi = new BigInteger("12345678923456");

How to convert the above "bi" to hexString.

Thanks
Regards
Nanaji
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-22-2008, 06:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
int i =2; String s = Integer.toHexString(i); System.out.printf("s = %s%n", s); BigInteger bi = new BigInteger("12345678923456"); System.out.printf("bi = %s%n", bi); System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE); System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE); long n = bi.longValue(); String hex = Long.toHexString(n); System.out.printf("hex = %s%n", hex);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-22-2008, 07:08 AM
Member
 
Join Date: May 2008
Posts: 27
nanaji is on a distinguished road
Send a message via MSN to nanaji Send a message via Yahoo to nanaji
Hi

Thanks for the quick reply. I really appreciate. It works fantastic.

Thanks hardWired.

Regards
Nanaji
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-22-2008, 07:27 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
It works like a charm but it is not very safe . BigInteger.longValue() returns only the lowest 64 bits while BigInteger is meant to represent arbitrary-precision integers. This means you can get a wrong value, not even of the same magnitude and it might be even with the wrong sign (+/-).
A simpler solution is to use BigInteger.toString(int radix).

Code:
import java.math.BigInteger; public class BIHex { public static void main(String[] args) { java.math.BigInteger bi = new BigInteger( "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890"); System.out.println("max long = " + Long.toHexString(Long.MAX_VALUE)); System.out.println("bi as long = " + Long.toHexString(bi.longValue())); System.out.println("bi as hex = " + bi.toString(16)); } }
When you run this class you see the difference:

Code:
max long = 7fffffffffffffff bi as long = accff196ce3f0ad2 bi as hex = 241fc1742fe8d29593a6afe52b31741cfe5a7f8e67e477381be47851641ef7bf14baccff196ce3f0ad2
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-22-2008, 07:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I have thing to clarify. As far as I remember, radix also have a range (a min value and a max value). What happened to the result if the radix is out of rang. I think if the radix out of that range, set to default, to decimal. So...
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-22-2008, 07:54 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
Hi Eranga

Yes you are right. The radix range is 2-36 and it is defined in Character.MIN_RADIX and Character.MAX_RADIX. For a radix out of range the transformation algorithm defaults to radix 10.
So... what? It works fine for radix 16
I am not sure I got the question. Is there any problem with my example?
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-22-2008, 08:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
I got what you say. So I remember that nice to see that.

What I'm say is, if the radix is 64?? (It can be, right?)
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-22-2008, 08:19 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
The max radix value is 36 because there are only 36 symbols to use for representation (26 letters + 10 digits). If you use 64 will default to 10 so you get the wrong representation. If you want to transform in base 64 you have to write your own algorithm and specify what symbols to use from 37 to 63.
__________________
Daniel @ [
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
]
Language is froth on the surface of thought
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-22-2008, 08:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, thanks for the explanation pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-22-2008, 02:37 PM
Member
 
Join Date: May 2008
Posts: 27
nanaji is on a distinguished road
Send a message via MSN to nanaji Send a message via Yahoo to nanaji
Hi ,




Regards
Nanaji

Last edited by nanaji : 05-23-2008 at 06:49 AM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-22-2008, 02:44 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What you have try to work on for loop there?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
BigInteger remainder results in zero perito New To Java 1 03-21-2008 06:07 PM
How to convert a string into a BigInteger valery New To Java 1 08-06-2007 10:36 PM
how to convert source code to xml valery XML 2 08-06-2007 10:29 PM
how to convert String number to int gabriel New To Java 3 08-02-2007 06:46 AM
Convert .java to .exe susan New To Java 1 07-25-2007 09:26 PM


All times are GMT +3. The time now is 12:04 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org