Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion from String to Byte

 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;)
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why you don't use a BigInteger

 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omar,

Thats a good suggestion, Thanks.

I was just trying to implement the long number arithmetic manually.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome ;)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic