Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hex String Manipulation in Java

Posted on 1997-04-30
3
Medium Priority
?
1,393 Views
Last Modified: 2008-03-10
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...
Thanks!
Sameer
sameer@dnrc.bell-labs.com
0
Comment
Question by:sameer043097
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219978
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];

res[0].ParseInt(MAC.substring(0,1);
res[1].ParseInt(MAC.substring(2,3);
...

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";
      break;
   case 14:
      res = "e";
      break;
...
   case 10:
       res= "a";
       break;
   default:
     String Tmp;
     Tmp.valueOf(hi);
      res = Tmp.toString();
}
switch (lo){
  case 15:
      res = res + "f";
      break;
   case 14:
      res = res + "e";
      break;
...
   case 10:
       res= res + "a";
       break;
   default:
     String Tmp;
     Tmp.valueOf(hi);
      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


0
 
LVL 2

Accepted Solution

by:
Philippe earned 300 total points
ID: 1219979

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: ");
    System.in.read(a);

    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[0]+
                   (char)hex[1]+
                   (char)hex[2]+
                   (char)hex[3]+
                   (char)hex[4]+
                   (char)hex[5]+ "<");
  }

}


Here is a test run:

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

  hope this helps,

    Philippe

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219980
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:


http://www.adventnet.com/docs.html
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses

610 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