Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to convert byte array to hex string

How to convert byte array to hex string

Scheduled Pinned Locked Moved Java
data-structureshelptutorialquestionlounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IICTECH
    wrote on last edited by
    #1

    Hi All, I got a piece of code from google to convert byte array to hex string...i din't understand few lines of code can any on ehelp me to understand ?? :) public static String CellKeysGeneration() { byte[] btba = new byte[5]; Random r = new Random(); r.nextBytes(btba); for (int i = 0; i < btba.length; i++) { btba[i] = btba[i]; } String str = tohexString(btba); return str; } public static String tohexString(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { sb.append(tohex(bytes[i] >> 4)); sb.append(tohex(bytes[i])); } return sb.toString(); } public static char tohex(int nibble) { final char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' , 'F' }; return hexdigit[nibble & 0xF]; } In above three functions, 1) I dint understand why in toHex function we are always performing "and" operartion with 0xF. 2) I dint understand why in tohexString function we are performing ">>" operation with 4. Can any one help me to understand toHexString and toHex functions ?? :) Thanks in advance !

    L 1 Reply Last reply
    0
    • I IICTECH

      Hi All, I got a piece of code from google to convert byte array to hex string...i din't understand few lines of code can any on ehelp me to understand ?? :) public static String CellKeysGeneration() { byte[] btba = new byte[5]; Random r = new Random(); r.nextBytes(btba); for (int i = 0; i < btba.length; i++) { btba[i] = btba[i]; } String str = tohexString(btba); return str; } public static String tohexString(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { sb.append(tohex(bytes[i] >> 4)); sb.append(tohex(bytes[i])); } return sb.toString(); } public static char tohex(int nibble) { final char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' , 'F' }; return hexdigit[nibble & 0xF]; } In above three functions, 1) I dint understand why in toHex function we are always performing "and" operartion with 0xF. 2) I dint understand why in tohexString function we are performing ">>" operation with 4. Can any one help me to understand toHexString and toHex functions ?? :) Thanks in advance !

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why have you reposted this question here? I already gave you a full explanation at While converting byte array to hexstring[^].

      One of these days I'm going to think of a really clever signature.

      I 1 Reply Last reply
      0
      • L Lost User

        Why have you reposted this question here? I already gave you a full explanation at While converting byte array to hexstring[^].

        One of these days I'm going to think of a really clever signature.

        I Offline
        I Offline
        IICTECH
        wrote on last edited by
        #3

        Hey im new to this forum stuff... well i could not locate the question i posted in the discussions.So i just re-posted it.. hope mistakes are accepted for the first time :-D by the way thanks for the reply.

        L 1 Reply Last reply
        0
        • I IICTECH

          Hey im new to this forum stuff... well i could not locate the question i posted in the discussions.So i just re-posted it.. hope mistakes are accepted for the first time :-D by the way thanks for the reply.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          IICTECH wrote:

          i could not locate the question i posted

          Hover your mouse over your name at the top right of the page and you will see links to your questions, answers, comments and forum messages. If you are new to the site then spend some time looking around to get familiar with it.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups