• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2091
  • Last Modified:

URGENT:assigning hex to char array

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
wrox
Asked:
wrox
  • 8
  • 6
  • 2
  • +1
1 Solution
 
CEHJCommented:
Assign them in a loop?
0
 
allahabadCommented:
What do mean dynamically ? Where will you derive your value for assignment ?
0
 
wroxAuthor Commented:
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!

 
CEHJCommented:
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
 
CEHJCommented:
What would you be doing with these chars afterwards though?
0
 
wroxAuthor Commented:
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
 
CEHJCommented:
Actually that should have been:


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

0
 
wroxAuthor Commented:
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
 
CEHJCommented:
>>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
 
wroxAuthor Commented:
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
 
CEHJCommented:
>>the numbers dont correspond..

Why not?
0
 
CEHJCommented:
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
 
CEHJCommented:
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
 
wroxAuthor Commented:
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
 
wroxAuthor Commented:
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
 
hj2k3Commented:
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
 
hj2k3Commented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 8
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now