?
Solved

URGENT:assigning hex to char array

Posted on 2003-03-12
17
Medium Priority
?
2,082 Views
Last Modified: 2007-12-19
hi

i need to pass hex data dynamically to a char[].

For eg.

char c[] = new char[12];
char c[0]= 0x7E;
char c[1]= ...
........ and so on, is possible.

How can i acheive the same dynamically?? as in without giving the hex values explicitly.
ne info wud be highly appreciated..
0
Comment
Question by:wrox
[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
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123195
Assign them in a loop?
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8123203
What do mean dynamically ? Where will you derive your value for assignment ?
0
 

Author Comment

by:wrox
ID: 8123413
dynamically as in say from a hex string

For eg. "00:02:2D:22:31:92" is a MAC address. I am trying to assign these in the following way

c[0]= 0x00;
c[1]= 0x02;
etc.

Of course this would mean hardcoding the MAC address which is not what i aim to do.I need to be able to assign different MAC addresses.
I can extract the individual strings 00,02,2D etc. but how do i go abt assigning them to the char[]??
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8123473
If you're using 1.4 you can do

String[] hexNums = addressString.split(":");
char[] c = new char[hexNums.length];
for (int i = 0;i < hexNums.length;i++) {
  c[i] = (char)Integer.parseInt(hexNums[i]);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123478
What would you be doing with these chars afterwards though?
0
 

Author Comment

by:wrox
ID: 8123483
dynamically as in say from a hex string

For eg. "00:02:2D:22:31:92" is a MAC address. I am trying to assign these in the following way

c[0]= 0x00;
c[1]= 0x02;
etc.

Of course this would mean hardcoding the MAC address which is not what i aim to do.I need to be able to assign different MAC addresses.
I can extract the individual strings 00,02,2D etc. but how do i go abt assigning them to the char[]??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123494
Actually that should have been:


c[i] = (char)Integer.parseInt(hexNums[i], 16);

0
 

Author Comment

by:wrox
ID: 8123534
i tried doing that..coz i have "00","02"...after splitting up the MAC string.
i cant use Integer.parseInt(hexNums[i]) bcoz u cant parse hexadecimal as int rite..so i used Integer.parseInt(hexNums[i],16) and then casted it to char type.

but then itz not the same as the original hex string

I need to do it this way coz I am using the WinPcap lib to send packets and i have to pass the MAC add in this way - char[] ..so what do i do??

ne ideas?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123584
>>but then itz not the same as the original hex string


Not sure what you mean by that. You'll have an array of numbers that correspond to each atom of your MAC address. Isn't that what you want?
0
 

Author Comment

by:wrox
ID: 8123674
Yepp taz the problem .. I cant get the array as i want it..the numbers dont correspond..

i just wanted to know whether it is possible to declare
c = 0x20 (/ any hex number)

in any other way and still be the same...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123689
>>the numbers dont correspond..

Why not?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123717
Hardcoding:

c[0]= (char)0x00;
c[1]= (char)0x02;

etc.

but i still don't see the problem with the array assignment from the tokens.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 120 total points
ID: 8123803
I'm not using 1.4 so i can't use split, but the following has exactly the same effect. It seems to me to have the right output does it not?


import java.util.StringTokenizer;

public class MacAddress {

  public static void main(String[] args) {
    char[] macAddress = getMacAddressAsCharArray("00:02:2D:22:31:92");
    for(int i = 0;i < macAddress.length;i++) {
      System.out.println("0x" + Integer.toHexString(macAddress[i]));
    }
  }


  public static char[] getMacAddressAsCharArray(String macAddress) {
    char[] atoms = new char[6];
    int ix = 0;
    StringTokenizer st = new StringTokenizer(macAddress,":");
    while (st.hasMoreTokens()) {
      atoms[ix] = (char)Integer.parseInt(st.nextToken(), 16);
      ix++;
    }
    return atoms;
  }

}

0
 

Author Comment

by:wrox
ID: 8123812
Yepp taz the problem .. I cant get the array as i want it..the numbers dont correspond..

i just wanted to know whether it is possible to declare
c = 0x20 (/ any hex number)

in any other way and still be the same...
0
 

Author Comment

by:wrox
ID: 8123943
Yepp taz the problem .. I cant get the array as i want it..the numbers dont correspond..

i just wanted to know whether it is possible to declare
c = 0x20 (/ any hex number)

in any other way and still be the same...
0
 
LVL 1

Expert Comment

by:hj2k3
ID: 8124262
when you say the numbers don't correspond, what do you mean? what is different from using c = 0x20 and c = (char)Integer.parseInt("20", 16)???
i have just tried it and it DOES give excactly the same character, e.g:

char c = 0xFE;
char c2 = (char)Integer.parseInt("FE", 16);
System.out.println(c);
System.out.println(c2);
System.out.println(c == c2);

last line prints 'true'

one strange thing though: when c=0x00, and i try and parse "00", i don't get any output whatsoever. maybe the problem is exculsively with zero, in which case you could check for zero and deal with it differently...
0
 
LVL 1

Expert Comment

by:hj2k3
ID: 8124280
just tried it again without the intermediate character output and it still prints 'true' when c=0x00; and with c2 = (char)Integer.parseInt("00", 16);

...so I don't know what/where the problem is
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 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