Results 1 to 6 of 6

Thread: Java - converting long into string format

  1. #1
    Join Date
    Jul 2009
    Posts
    188

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

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Java - converting long into string format

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

  3. #3
    Join Date
    Jul 2009
    Posts
    188

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

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Java - converting long into string format

    Hello
    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.
    byte0 = addresseLong & 0xFF;
    byte1 = addresseLong & 0xff00>> 8;
    Byte2 = addresseLong & 0xFF0000>> 16;
    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\\

  5. #5
    Join Date
    Jul 2009
    Posts
    188

    Re: Java - converting long into string format

    Hi
    Ok, thank you
    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:
    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];

  6. #6
    Join Date
    May 2008
    Posts
    2,389

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

Similar Threads

  1. Replies: 5
    Last Post: 19-04-2011, 07:32 PM
  2. Converting Boolean values to string in Java
    By 27lynx27 in forum Software Development
    Replies: 1
    Last Post: 13-02-2011, 03:40 AM
  3. Tips for converting DVD format to iRiver format.
    By Computer_Freak in forum Tips & Tweaks
    Replies: 2
    Last Post: 11-02-2010, 06:28 AM
  4. Tips for converting M2TS format to DVD format
    By Computer_Freak in forum Tips & Tweaks
    Replies: 4
    Last Post: 06-02-2010, 01:31 AM
  5. Converting string to char
    By Abdullah30 in forum Software Development
    Replies: 3
    Last Post: 03-09-2009, 11:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,046,037.90001 seconds with 17 queries