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

# 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
• 8
• 6
• 2
• +1
1 Solution

Commented:
Assign them in a loop?
0

Commented:
What do mean dynamically ? Where will you derive your value for assignment ?
0

Author 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

Commented:
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

Commented:
What would you be doing with these chars afterwards though?
0

Author 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

Commented:
Actually that should have been:

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

0

Author 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

Commented:
>>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 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

Commented:
>>the numbers dont correspond..

Why not?
0

Commented:
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

Commented:
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) {
for(int i = 0;i < macAddress.length;i++) {
}
}

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

Author 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

Commented:
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

Commented:
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

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