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]
# 1
Try integer.toHexString for each byte plus your padding, then concatenate in a StringBuilder with the colon as delimiter. in the end, just delete the last colon.
CeciNEstPasUnProgrammeura at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 2
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);
sabre150a at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 3
> I'm not sure I understand but is this what you want?Even better.
CeciNEstPasUnProgrammeura at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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:");

sabre150a at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 5
Great!!!!Thanks for the help....but how do I drop the last colon?01:02:03:
javaroba at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 6
RTFAPI. There's a deleteCharAt() method. Or just don't add it if your loop counter is at array-length - 1.
CeciNEstPasUnProgrammeura at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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

sabre150a at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 8
Thank you everyone for your help...I think at this point, I'm going to take your advice and dig into the APIs.This is a great forum... thanks everyone... talk to you later.
javaroba at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 9
The unresolved question is: did he use my approach or yours? In your case, there's no deleteCharAt(). :)
CeciNEstPasUnProgrammeura at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...
# 10
I used sabres..... works great.
javaroba at 2007-7-7 17:07:14 > top of Java-index,Java Essentials,Java Programming...