aspose file tools*
The moose likes Java in General and the fly likes Conversion from String to Byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conversion from String to Byte" Watch "Conversion from String to Byte" New topic
Author

Conversion from String to Byte

Nathan Heimdall
Greenhorn

Joined: Mar 02, 2009
Posts: 20
I have hexadecimal string "AAAA"
I am mapping it to its binary equivalent string manually ( A=1010)
and then converting the string to a byte array.

The problem is that the contents of the byte array are not 1's and 0's but their ASCII equivalents (49 and 48).
( I have a structure to add two such byte arrays as 1's and 0's)

What am I doing wrong?

Thanks,

-Nathan
"A single conversation with a wise man is better than ten years of study."
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
well by byte array you mean a byte[] and a byte is 8 bits so you will not get the 1's and 0's. however you could use the Integer.toBinaryString(int) method to obtain the binary representation of each byte in the array.

there is no java type that can have a binary value, unless you do your own implementation, may be an array of booleans ;)


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

omar al kababji wrote:there is no java type that can have a binary value, unless you do your own implementation, may be an array of booleans ;)

You mean java.util.BitSet?
Although it's toString does not print the binary value but only the set (1) indexes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Can you explain in more detail what you goal is?

First you say that you are mapping "AAAA" to "its binary equivalent string", "1010". And then you are surprised that the string contains ASCII characters 49 and 48. But that's exactly what you converted it to?

So, what exactly do you want to achieve?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nathan Heimdall
Greenhorn

Joined: Mar 02, 2009
Posts: 20
My intention is to add 2 long hexadecimal strings ( like "AAAAAAAAAAAA....100 times" + "BBBB...100 times")

I can't convert it directly to integer using parseInt method due to the long length, I get an overflow.

So I wanted to convert them into binary strings and then add them bit by bit and then handle the carry.For which I wanted to convert the
hex strings to binary strings.

I just subtracted 48 from the values in the byte array to get the binary equivalent
and then added them.

Thanks for your help guys, Appreciate it.

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
why you don't use a BigInteger

Nathan Heimdall
Greenhorn

Joined: Mar 02, 2009
Posts: 20
Omar,

Thats a good suggestion, Thanks.

I was just trying to implement the long number arithmetic manually.
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
You are welcome ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion from String to Byte