-1

I want to convert any length of String to byte32 in Java.

Code

String s="9c46267273a4999031c1d0f7e40b2a59233ce59427c4b9678d6c3a4de49b6052e71f6325296c4bddf71ea9e00da4e88c4d4fcbf241859d6aeb41e1714a0e";

//Convert into byte32
Priyanka Kanse
  • 1,051
  • 1
  • 15
  • 27
  • 1
    Possible duplicate of [How to convert string to byte in Java](https://stackoverflow.com/questions/8652804/how-to-convert-string-to-byte-in-java) – Logan Jun 05 '18 at 08:40
  • 5
    What is 'byte32' ? – Ryan Leach Jun 05 '18 at 08:42
  • I don't know what a byte32 is but how are you expecting this to work? A fixed amount of bytes can only hold a fixed amount of data. If your string is 1 trillion characters, how are you expecting it to fit in a (presumably) relatively small number of bytes? It will necessarily be a lossy conversion (like a hash). – Michael Jun 05 '18 at 08:46
  • Please refer to this topic, see the answer by wannamit: https://ethereum.stackexchange.com/questions/11898/how-to-convert-java-string-to-bytes32-in-java-i-am-using-web3j-solidity-wrappe – infiniti24 Jun 05 '18 at 08:47
  • Use an appropriate hashing function, e.g. sha2. – Henrik Aasted Sørensen Jun 05 '18 at 08:48
  • 2
    You can't put arbitrarily large data in exactly 32 bytes in a non-lossy way, that's simply not possible. Do you want a hash or is your input limited in some way? For example the example input you show seems to be a hex string of 124 characters. Will that always be the case? – Joachim Sauer Jun 05 '18 at 08:49
  • 1
    Would you mind showing us the expected output? Do you want `"9c46267273a4999031c1d0f7e40b2a59"` as the result? Or `"œF&rs¤™.1ÁÐ÷ä.*Y"` or `"œF&rs¤™.1ÁÐ÷ä.*Y#<å”'Ĺg.l:Mä›'R"`? – Thomas Weller Jun 05 '18 at 08:50
  • @JoachimSauer I am open to any algorithm or encryption just as when I store in blockchain which cost me for every length so I need to fixed32 so it reduces my cost – Priyanka Kanse Jun 05 '18 at 08:52
  • @ThomasWeller but can i convert again in same string? – Priyanka Kanse Jun 05 '18 at 08:53
  • 1
    It sounds like you are not even sure of what you are doing, so for gods sake, don't mess with Crypto! – Ryan Leach Jun 05 '18 at 08:53
  • Phew... with your current understanding of strings, bytes and byte32, you should neither encryption, hashing, blockchains nor compression. – Thomas Weller Jun 05 '18 at 08:54
  • @RyanTheLeach I guess if you know the answer please help me with that. – Priyanka Kanse Jun 05 '18 at 08:55
  • I **don't** know the answer, but I'm also not writing Crypto. – Ryan Leach Jun 05 '18 at 08:56
  • @ThomasWeller just i need simple conversion of any length of string to fixed32 bytes .Can you help me in providing any algorithm or approach – Priyanka Kanse Jun 05 '18 at 08:56
  • @RyanTheLeach this is simple java question i am not asking about crypto just simple want to simple string of any length to fixed32 byte. – Priyanka Kanse Jun 05 '18 at 08:57
  • It's not possible. What you want is compression, but there is no compression that can guarantee 32 byte size. Otherwise every ZIP file would have 32 byte size – Thomas Weller Jun 05 '18 at 08:57
  • 1
    @PriyankaKanse there is no algorithm to achieve what you want. Converting a string with 1 million characters to 32 bits - not possible. It is possible, but there is no way you can get back the 1 million characters. So the answer is NO. – Ashwin K Kumar Jun 05 '18 at 08:59
  • 1
    This sounds like an [X-Y problem](http://xyproblem.info/). What exactly do you need the 32-byte array for? What will you do with it once you obtain it? – DodgyCodeException Jun 05 '18 at 09:29
  • @DodgyCodeException basically byte32 give me cheaper expense while storing the array as compared to string with length 120. So I just want to save some cents for every transaction. – Priyanka Kanse Jun 05 '18 at 10:24
  • But will you need to reconstruct the original string from the 32-byte array (I assume that's what you mean by this strange term "byte32") at some later time? – DodgyCodeException Jun 05 '18 at 10:30
  • Ah, hold on... I've just googled "byte32". Are you using Solidity and Ethereum? If so, why didn't you say so? You are asking general Java people about a specific technology that they haven't encountered before. – DodgyCodeException Jun 05 '18 at 10:41

3 Answers3

3

From the comments it became clear that you want to reduce the storage space of that string to 32 bytes.

The given string can easily be compressed from the 124 bytes to 62 bytes by doing a hexadecimal conversion.

However, there is no algorithm and there will not be an algorithm that can compress any data to 32 bytes. Imagine that would be possible: it would have been implemented and you would be able to get ZIP files of just 32 bytes for any file you compress.

So, unfortunately, the answer is: it's not possible.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

You can not convert any length string to a byte array of length 32.

Java uses UTF-16 as it's string encoding, so in order to store 100% of the string, 1:1 as a fixed length byte array, you would be at a surface glance be limited to 16 characters.

If you are willing to live with the limitation of 16 characters, byte[] bytes = s.getBytes(); should give you a variable length byte array, but it's best to specify an explicit encoding. e.g. byte [] array2 = str.getBytes("UTF-16");

This doesn't completely solve your problem. You will now likely have to check that the byte array doesn't exceed 32 bytes, and come up with strategies for padding, possible null termination (which may potentially eat into your character budget)

Now, if you don't need the entire UTF-16 string space that Java uses for strings by default, you can get away with longer strings, by using other encodings.

IF this is to be used for any kind of other standard or something ( I see references to etherium being thrown around) then you will need to follow their standards.

Unless you are writing your own library for dealing with it directly, I highly recommend using a library that already exists, and appears to be well tested, and used.

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
-2

You can achieve with the following function

byte[] bytes = s.getBytes();