Java - converting long into string format
Hello,
I have a variable of type Long contains a MAC address (a number between 0x000 and 0xFFF)
How to convert this number into a string of the form: xx: xx: xx: xx: xx: xx
Xx represents the hexadecimal value decomposition of the MAC address
eg
Quote:
If my variable is 0x010203040506
I want the string "01:02:03:04:05:06"
also, when I do this:
Code:
Long temp = Long.parseLong(varSTR, 16);
If the string is not a number that generates an error to me, how to avoid this?
Re: Java - converting long into string format
Hello,
Quote:
How to convert this number into a string of the form: xx: xx: xx: xx: xx: xx
By hand you down into your bytes long and you separately displays each byte in hex
Quote:
Long temp = Long.parseLong(varSTR, 16);
If the string is not a number that generates an error to me, how to avoid this?
If you mean "how to parse a mac address", another time it by hand. You splitter around "" your chain, and each element of the split, did you spend in Integer.parseInt (element 16) to hexadecimal parsing.
Re: Java - converting long into string format
Hi
Ok, thank you, I thought there were all made for functions. To parse a long in byte [], it is how (the Byte class knows that parse String)?
Quote:
If you mean "how to parse a mac address", another time it by hand. You splitter around "" your chain, and each element of the split, did you spend in Integer.parseInt (element 16) to hexadecimal parsing.
No, what I would do is test if element can be parsed to int before using the FUNCTIONS (to avoid having an error).
Re: Java - converting long into string format
Hello
Quote:
To parse a long in byte [], it is how (the Byte class knows that parse String)?
Do not parse, convert it. Simply extract the bits that interest you.
Quote:
byte0 = addresseLong & 0xFF;
byte1 = addresseLong & 0xff00>> 8;
Byte2 = addresseLong & 0xFF0000>> 16;
Quote:
No, what I would do is test if element can be parsed to int before using the FUNCTIONS (to avoid having an error)
With a regular expression, for example:
Code:
adressesString.games("^\\s * (\\(p) xdigit\\xdigit (p)) (5)\\(p) xdigit\\(p) xdigit\\
Re: Java - converting long into string format
Hi
Ok, thank you
Quote:
byte0 = addresseLong & 0xFF;
byte1 = addresseLong & 0xff00>> 8;
Byte2 = addresseLong & 0xFF0000>> 16;
It's super heavy all these shifts and these masks are not simple? In C it would have been just that:
Quote:
char exby[4];
long myVal = 0x123456; / / (Size 8 bytes, C)
exby[0] = ((char*)& myVal )[0];
exby[1] = ((char*)& myVal )[1];
exby[2] = ((char*)& myVal )[2];
exby[3] = ((char*)& myVal )[3];
Re: Java - converting long into string format
Hi
No, your first C code is wrong, a long-does not necessarily be of 8 byte in C, its minimum size is 32 bits, but this can vary depending on the architecture and compiler used. Then, more seriously, your code assumes that the bytes in a long endiance are in focus, which is not guaranteed depending on the architecture used. In your case, if I run your code on x86, I get "56 34 12" on HP-UX/PA-RISC get "0 12 34. Conversely, a code with the mask gives assurance that it is always the same result. Finally, this code turns arithmetic on the pointer, there is no pointer in Java. If you want to be "shorter", you got the code below
Quote:
byte[] x = new byte[8];
long val = ....;
for (int i = 0; I < 8; I + +)
x[i]= val &(0xFF <<(i *8))>> i *8
but is less readable and less efficient.