-5

How can I convert a hex value "0000.0012.13a4" into "00:00:00:12:13:A4"?

cs95
  • 379,657
  • 97
  • 704
  • 746
chaitanya
  • 11
  • 1
  • 7

5 Answers5

1
text = '0000.0012.13a4'
text = text.replace('.', '').upper()   # a little pre-processing

# chunk into groups of 2 and re-join
out = ':'.join([text[i : i + 2] for i in range(0, len(text), 2)])   
print(out)

00:00:00:12:13:A4
cs95
  • 379,657
  • 97
  • 704
  • 746
0
import re

old_string = "0000.0012.13a4"

new_string = ':'.join(s for s in re.split(r"(\w{2})", old_string.upper()) if s.isalnum())

print(new_string)

OUTPUT

> python3 test.py
00:00:00:12:13:A4
>

Without modification, this approach can handle some other MAC formats that you might run into like, "00-00-00-12-13-a4"

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

Try following code

import re
hx = '0000.0012.13a4'.replace('.','')
print(':'.join(re.findall('..', hx)))

Output: 00:00:00:12:13:a4
hardika
  • 572
  • 5
  • 17
-1

There is a pretty simple three step solution:

First we strip those pesky periods.

step1 = hexStrBad.replace('.','')

Then, if the formatting is consistent:

step2 = step1[0:2] + ':' + step1[2:4] + ':' + step1[4:6] + ':' + step1[6:8] + ':' + step1[8:10] + ':' + step1[10:12]
step3 = step2.upper()

It's not the prettiest, but it will do what you need!

Community
  • 1
  • 1
-2

It's unclear what you're asking exactly, but if all you want is to make a string all uppercase, use .upper()

Try to clarify your question somewhat, because if you're asking about converting some weirdly formatted string into what looks like a MAC address, we need to know that to answer your question.

Jordan Singer
  • 567
  • 5
  • 11
  • I want this mac address 0000.0012.13a4 to be converted to 00:00:00:12:13:A4. Changing period to colon and decimal spacing from every four digits to two. They both are same mac addresses. But I am looking for proper formatting – chaitanya Sep 11 '17 at 02:25