?
Solved

String (MAC Address) to Byte Array

Posted on 2005-05-11
28
Medium Priority
?
5,039 Views
Last Modified: 2009-07-20
Hi,

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

Eg.
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....

Vicky
0
Comment
Question by:vks_vicky
  • 11
  • 7
  • 6
  • +2
28 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13976480
byteArray[0] = Byte.parseByte(hexString.substring(0, 2), 16);
byteArray[1] = Byte.parseByte(hexString.substring(2, 4), 16);
...
0
 
LVL 92

Expert Comment

by:objects
ID: 13976486
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);
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 13976552
BigInteger bi = new BigInteger("111111111A12", 16);
byte[] bytes = bi.toByteArray();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Author Comment

by:vks_vicky
ID: 13977169
CHEJ ur code works fine, but on using different MAC Address like abcdef121001, i get -ve values, any idea of how to resolve these....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13977187
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
0
 
LVL 5

Author Comment

by:vks_vicky
ID: 13977216
objects using ur methor i get error ....

"java.lang.IllegalStateException
      org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:350)"
0
 
LVL 5

Author Comment

by:vks_vicky
ID: 13977293
CHEJ, it works like charm. Tnx.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13977876
:-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13981817
vks_vicky,
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.
0
 
LVL 20

Expert Comment

by:a_b
ID: 24892885
@aozarov:
Opening an old thread....but is there any work around to the leading zeroes issue?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24892904
What makes you ask a_b?
0
 
LVL 20

Expert Comment

by:a_b
ID: 24892963
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 24892986
thats right, you can't use that with leading zeroes (without manually correcting)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893005
It's not a problem - you know you're expecting a six-byte length
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893016
String mac = String.format("%012X", bi.longValue());
0
 
LVL 92

Expert Comment

by:objects
ID: 24893081
> 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.

0
 
LVL 20

Expert Comment

by:a_b
ID: 24893183
@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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893208
>>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?
0
 
LVL 92

Expert Comment

by:objects
ID: 24893242
rotfl, someones head is stuck in the sand ;)
0
 
LVL 20

Expert Comment

by:a_b
ID: 24893297
@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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893307
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893328
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

0
 
LVL 20

Expert Comment

by:a_b
ID: 24893334
Thanks.....I think I need a coffee break.....my head is really stuck in the gutter..........feeling kinda stupid right now........
0
 
LVL 92

Expert Comment

by:objects
ID: 24893340
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 24893347
convert to a byte array
create a second byte array
copy first byte array into new byte array

or

create byte array
loop to populate array

simple choice :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893375
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
0
 
LVL 20

Expert Comment

by:a_b
ID: 24893395
I resolved the issue. I was starting with  a String mac address, converting to byte  to do some binary operations on them.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24893411
Good :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question