Link to home
Start Free TrialLog in
Avatar of wrox
wrox

asked on

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..
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Assign them in a loop?
Avatar of allahabad
allahabad

What do mean dynamically ? Where will you derive your value for assignment ?
Avatar of wrox

ASKER

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[]??
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]);
}
What would you be doing with these chars afterwards though?
Avatar of wrox

ASKER

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[]??
Actually that should have been:


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

Avatar of wrox

ASKER

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?
>>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?
Avatar of wrox

ASKER

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...
>>the numbers dont correspond..

Why not?
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wrox

ASKER

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...
Avatar of wrox

ASKER

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