Hex String Manipulation in Java

I have this problem:
I wish to take a string from the user. It represents the Pysical Address in Hex (say Mac address).
To pass this on to an SNMP agent, I need to send it as type STRING of 6 OCTETS. So if the user inputs an address in hex as: "aa bb cc dd ee ff", How do I
pack this correctly into 6 OCTETS correctly so as to be interpreted as a PhyAddress by the SNMP agent?
PS: I am using JDK 1.0.2 and have been unable to find a suitable method to do this...
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

PhilippeConnect With a Mentor Commented:

Hi Sameer,

JDK 1.1 has a Byte wrapper with a method called decode which should do what you want. In the meantime you have to hand-code it eg. like this:

public class hex {
  static int i;
  public static void main(String argv[]) throws java.io.IOException{
    byte[] a=new byte[12], hex=new byte[6];

    System.out.println("enter six bytes as 12 hex digits: ");

    for (i=0 ; i<a.length; i++) {
      // upper_case:
      if (a[i] > 'F')  a[i] = (byte)(a[i]-'a'+'A');
      // hex_digit:
      if (a[i] >= '9') a[i] = (byte)(a[i]-'A'+10);
                  else a[i] = (byte)(a[i]-'0');
      // hex byte
      if ((i % 2) == 1)      hex[i/2] = (byte)(a[i-1]*16 + a[i]);

    System.out.println("here are your bytes in ascii: >"+
                   (char)hex[5]+ "<");


Here is a test run:

darkstar/java: java hex
enter six bytes in 12 hex digits:
here are your bytes in ascii: >HELLO <

  hope this helps,


I don't quite understand your question, which 6 octets are you refering to, the PDU's maybe? And exactly what kind of physical address output are you looking for?

Some cooments:

MAC addresses are used only by ARP and RARP and are defined as NIC interfaces only. Whats more, MAC addresses can and usualy are of diferent lengths depending on what type of Ethernet NIC you are using (see RFC 826 An Ethernet Address Resolution Protocol). Your 96 bit example is used only by 10Mbit Ethernet cards.

What's more, MAC addresses will only allow you to locate IP address on boxes that actualy have an Ethernet adapter installed and on the same subnet. (This excludes all of Token-ring, SNA, and modems).

RARP (RFC 904), which is the other MAC associated protocol is used only to determine a NIC's own IP address on diskless work stations.

On the other hand, SNMP uses IP addresses (either in dot notation or full 64 bit address).

Partial answer:

If you are allready aware of all these limitations, then for as much of yuor question as I can interpret, here is a partial answer:

Java handles hex values via excape sequences as character literals in the form \xdd.

This implies you can parse your string into to hex character pairs and stor the values in integer variables with a code structure like:

String MAC;
int res[6];


To convert back to hex or oct "string values" you will have to define a method. Since the code fragment above packs the values into neat byte size bundles, converting to octal or hex should not be a problem using the modulus operator (%). For example, to convert back to hex you can use:

String res;

hi = (int) intval/16;
lo=intval % 16;

switch (hi){
   case 15:
      res = "f";
   case 14:
      res = "e";
   case 10:
       res= "a";
     String Tmp;
      res = Tmp.toString();
switch (lo){
  case 15:
      res = res + "f";
   case 14:
      res = res + "e";
   case 10:
       res= res + "a";
     String Tmp;
      res = res + Tmp.toString();

Once res contains the hex pair, you can return it to the calling method and build a result string with that.

For octal values the solution is similar, just use division and modulo 8, and generate 3 values instead of two.

I hope I understood your question properly and gave you a coheerent suggestion

Not to compete with philippe's answer, and it doesn't address your question directly; but I just found a comertial SNMP class library you might find interesting at:

All Courses

From novice to tech pro — start learning today.