String (MAC Address) to Byte Array


I have this hex string, "111111111A12", (basicall it a MAC Address). I wanna convert it into a byte array.

String hexString = "111111111A12";
byte byteArray[] = new byte[6];

the value of byteArray should be...
byteArray[0] = (byte)0x11;
byteArray[1] = (byte)0x11;
byteArray[2] = (byte)0x11;
byteArray[3] = (byte)0x11;
byteArray[4] = (byte)0x1A;
byteArray[5] = (byte)0x12;

I tried doing this using ...., but no success

int val = Integer.parseInt( hexString, 16 ); --> I thing i'm wrong here (i've tried diff. value, ain't know what i'm doing here, waste 2 days fro this :) )

byteArray[0] = (byte) ( ( val >>> 40 ) & 0xff );
byteArray[1] = (byte) ( ( val >>> 32 ) & 0xff );
byteArray[2] = (byte) ( ( val >>> 24 ) & 0xff );
byteArray[3] = (byte) ( ( val >>> 16 ) & 0xff );
byteArray[4] = (byte) ( ( val >>> 8 ) & 0xff );
byteArray[5] = (byte) ( ( val >>> 0 ) & 0xff );

But it does not help. Any suggestions....

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
byteArray[0] = Byte.parseByte(hexString.substring(0, 2), 16);
byteArray[1] = Byte.parseByte(hexString.substring(2, 4), 16);
Mick BarryJava DeveloperCommented:
or do it in a loop

for (int i=0; i<byteArray.length; i++)
  byteArray[i] = Byte.parseByte(hexString.substring(i*2, (i+1)*2), 16);
BigInteger bi = new BigInteger("111111111A12", 16);
byte[] bytes = bi.toByteArray();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

vks_vickyAuthor Commented:
CHEJ ur code works fine, but on using different MAC Address like abcdef121001, i get -ve values, any idea of how to resolve these....
Yes, byte in Java is a signed value, so you need to 'unsign' it:

int i = whateverNegativeByte & 0xFF;

You should do that for each element anyway actually
vks_vickyAuthor Commented:
objects using ur methor i get error ....

vks_vickyAuthor Commented:
CHEJ, it works like charm. Tnx.
you need to be aware that BigInteger supress leading zeros.
So new BigInteger("00000abbccdd", 16).toByteArray().length(); which is a valid MAC address will return you 4 bytes and not 6 as you might expect.
Opening an old thread....but is there any work around to the leading zeroes issue?
What makes you ask a_b?
BigInteger bi = new BigInteger("000111111A12", 16);
byte[] bytes = bi.toByteArray();

My mac address has leading zeros that disappear the when I use your code snippet.
Mick BarryJava DeveloperCommented:
thats right, you can't use that with leading zeroes (without manually correcting)
It's not a problem - you know you're expecting a six-byte length
String mac = String.format("%012X", bi.longValue());
Mick BarryJava DeveloperCommented:
> My mac address has leading zeros that disappear the when I use your code snippet.

thats why that technique is often avoided in the real world, all the stuffing around required to make it work for all cases defeats the purpose so usually simpler to just loop.

@CEJH: String.format("%012X", bi.longValue()); does not solve my problem as I need to have bytes have the correct info. Any ideas on how to do that?
>>as I need to have bytes have the correct info.

I'm not sure what you mean by that unfortunately. What exactly is the problem?
Mick BarryJava DeveloperCommented:
rotfl, someones head is stuck in the sand ;)
@CEJH: What I meant was when I execute BigInteger bi = new BigInteger(macStr,16); the leading zeroes are already gone by the time I come to my next set of operations which is converting to bytes. So now, the bytes are not aware of the fact that there were leading zeros.
You're making what's known as a 'category error'. Numbers have no concept of 'leading zeros' - only strings do. What you need to do is format the number as a string, which is what my code does
If otoh you're working with arrays, you could do something like:
byte[] destination = new byte[6];
int ix = destination.length - source.length;
for (byte b : source) {
	destination[ix++] = b;

Open in new window

Thanks.....I think I need a coffee head is really stuck in the gutter..........feeling kinda stupid right now........
Mick BarryJava DeveloperCommented:
Which is exactly one of the reason *not* for using it. If you're going to loop anyway just calculate the values in the loop. Not to mentioned creating a second byte array for a simple conversion.
Mick BarryJava DeveloperCommented:
convert to a byte array
create a second byte array
copy first byte array into new byte array


create byte array
loop to populate array

simple choice :)
a_b, it would help if i knew exactly what you need to do. I don't know whether you need it as a String or what or even what you're starting with
I resolved the issue. I was starting with  a String mac address, converting to byte  to do some binary operations on them.
Good :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.