Adding colons.
I'm using the below code... I want to add colons between each byte, excluding the end.
(example: 0a:23:12:)
How can I accomplish this?
BigInteger biTM =new BigInteger(text_array_bytes);
// Format to binary
String text_msg = biTM.toString(2);
// Format to hexadecimal
text_msg = biTM.toString(16);
if (text_msg.length() % 2 != 0)
{
// Pad with 0
text_msg ="0" + text_msg;
}
[690 byte] By [
javaroba] at [2007-11-26 13:03:09]

> I'm not sure I understand but is this what you want?
> String text_msg =
> biTM.toString(16).replaceAll("\\p{XDigit}\\p{XDigit}",
> "$0:");
> System.out.println(text_msg);
I have just understood a bit better. I think you need
String text_msg = biTM.toString(16);
text_msg = (text_msg.length() %2) == 0? text_msg : "0" + text_msg;
text_msg = text_msg.replaceAll("\\p{XDigit}\\p{XDigit}","$0:");
> Great!!!!
>
> Thanks for the help....
>
> but how do I drop the last colon?
>
> 01:02:03:
My fault! I had a bad regex. TryString text_msg = biTM.toString(16);
text_msg = (text_msg.length() %2) == 0? text_msg : "0" + text_msg;
text_msg = text_msg.replaceAll("\\p{XDigit}\\p{XDigit}(?=\\p{XDigit})","$0:");
Message was edited by:
sabre150