I need to prepend the string “00” or byte 0x00 to the beginning of a byte array? I tried to do it with a for loop but when I convert it to hex it doesn’t show up in the front.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The string “00” is different than the number 0x00 when converted to Bytes. What is the data type you are trying to prepend to your byte array? Assuming it’s a Byte representation of the string “00”, try the following:
or you can use Arrays.copy(…) instead of the two for loops as mentioned before to do the prepending. See How to combine two byte arrays
Alternativly, if you are trying to literally prepend 0 to your byte array, decalare
prependBytesin the following way and use the same algorithmAlso you say that you’re converting your byte array to hex, and that may truncate leading zeros. To test this, try prepending the follwoing and converting to hex and see if there is a different output:
If it is removing the leading zeros that you want, you may wish to convert your hex number to a string and format it.