This discussion is archived
1 2 3 4 Previous Next 58 Replies Latest reply: Nov 27, 2006 1:40 AM by 791266 RSS

Convert decimal to hexadecimal.

807607 Newbie
Currently Being Moderated
Hello, i need to convert an array of bytes in decimal to an array of bytes in hexadecimal.

For example:
byte[] arrayDec={(byte)2,(byte)5,(byte)5}
to
byte[] arrayHex={(byte)F,(byte)F}


Thanks.
  • 1. Re: Convert decimal to hexadecimal.
    791266 Explorer
    Currently Being Moderated
    Hi,

    There's nothing to convert. Decimal, Hexadecimal etc are just different ways to display data. It's still binary in memory.

    Why do you think you need to convert? Is it to display the data?

    Kaj
  • 2. Re: Convert decimal to hexadecimal.
    791266 Explorer
    Currently Being Moderated
    Here's an example which displays the data in hex.
    public class Test {
         public static void main (String[] args) {
              byte[] data = new byte[] { 8, 9, 10, 11, 12 };
              for (byte b : data) {
                   System.out.printf("%1x\n", b);
                   //System.out.println(Integer.toHexString(b));
              }
         }
    }
    Either use printf or Integer.toHexString

    kaj
  • 3. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    I need a function that change an array of bytes in decimal to an array of bytes in hexadecimal for the school. I can't use any function of java, I have to find an algorithm that do this. Only i can manage bytes or integer.
  • 4. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    This looks to be related to the problem in - http://forum.java.sun.com/thread.jspa?threadID=787436 . It should give you an idea of what you have to do.
  • 5. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated

    hi alfrodo this can helpful to you try it out..
    public class convert     {
    
    public static void main(String args[])     {
    
    int deci=15;
    System.out.println(hexaDecimal(deci));
    
    }
    
    
    public static String hexaDecimal(int h)
        {
             int rem;
             String output="";
             String digit;
             String backwards="";
             
             do
             {              
                  rem=h%16;
                  digit=DecimalToHexa(rem);
                  h=h/16;
                  output+=digit;
             }
             while(h/16!=0);
             
             rem=h%16;
             
             digit=DecimalToHexa(rem);
             
             output=output+digit;
             
             for(int i=output.length()-1; i>=0; i--)
             {
                  backwards+=output.charAt(i);
             }
             
             return backwards;
        }
        
        
        public static String DecimalToHexa(int rem)
        {          
             
             String str1=String.valueOf(rem);
             
                  if(str1.equals("10"))
                       str1="A";
                  
                  else if(str1.equals("11"))
                       str1="B";
                  
                  else if(str1.equals("12"))
                       str1="C";
                  
                  else if(str1.equals("13"))
                       str1="D";
                  
                  else if(str1.equals("14"))
                       str1="E";
                  
                  else if(str1.equals("15"))
                       str1="F";
                  
                  else
                       str1=str1;
       
             
             return str1;
        }
    
    }
  • 6. Re: Convert decimal to hexadecimal.
    791266 Explorer
    Currently Being Moderated
    I need a function that change an array of bytes in
    decimal to an array of bytes in hexadecimal for the
    school. I can't use any function of java, I have to
    find an algorithm that do this. Only i can manage
    bytes or integer.
    That can't be done. I have already told you that a byte is a byte. You can't convert a byte from decimal to hexadecimal since it always is binary. Octal, hexadecimal, decimal is just a matter of representation.

    See the code that I posted. It displays the bytes in hexadecimal.

    Kaj
  • 7. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    I have decide I don't understand you problem. What result should these two give

    byte[] arrayDec={(byte)3,(byte)5,(byte)5}

    byte[] arrayDec={(byte)9,(byte)9,(byte)9}
  • 8. Re: Convert decimal to hexadecimal.
    791266 Explorer
    Currently Being Moderated
    Ah, Now I understand. I should have checked the posted arrays and not read the text and topic. :)
  • 9. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    Yes, it's like to that thread, but i don�t understand the code, and i need to do the opposite.
  • 10. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    See the code that I posted. It displays the bytes in
    hexadecimal.
    Hi Kaj. I think the OP want to convert an arrray of something like BCD to a compact byte representation. The problem as I see it is that he has not given us enough information about the allowable domain. This looks to be similar to one problem I dealt with about a week ago but until the OP gives more details I can't be sure.

    :-( Bit slow with this.

    Message was edited by:
    sabre150
  • 11. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    Yes, it's like to that thread, but i don�t understand
    the code, and i need to do the opposite.
    You need to study the code unttil you do understand it and then implement the inverse algorithm that I sketched. I'm not going to do it for you.
  • 12. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    My problem is that i have an array of bytes that represent a decimal number:

    byte[] decimal={(byte)2,(byte)5,(byte)5}, for example.

    and i want an array of bytes that represent that number in hexadecimal:

    byte[] decimal={(byte)F,(byte)F}

    That is to say: 255 (decimal) -> FF (hexadecimal)

    I don�t want you do nothing for me Sabre150, i only need help for solve the problem, and i don�t understand your code.

    Message was edited by:
    Alfrodo
  • 13. Re: Convert decimal to hexadecimal.
    791266 Explorer
    Currently Being Moderated
    @Op. Do you have any constraints on how many bytes there might be in the input array?
  • 14. Re: Convert decimal to hexadecimal.
    807607 Newbie
    Currently Being Moderated
    My problem is that i have an array of bytes that
    represent a decimal number:

    byte[] decimal={(byte)2,(byte)5,(byte)5}, for
    example.

    and i want an array of bytes that represent that
    number in hexadecimal:

    byte[] decimal={(byte)F,(byte)F}

    That is to say: 255 (decimal) -> FF (hexadecimal)
    This is just a re-statement of the original post. You have not answered the questions I posed.
    I don�t want you do nothing for me Sabre150, i only
    need help for solve the problem, and i don�t
    understand your code.
    I don't see how you can expect to understand the algorithm in just 1 minute, You need to put in some serious effort including working with the hex to bcd code I posted. Until I see some effort I will not help further.
1 2 3 4 Previous Next