Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Char array to a String array with hex

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Char array to a String array with hex

    Hi Guys,

    I have a string array but each cell in the 1d string array stores each character

    the text file is :

    "START START START
    The quick brown fox jumps over the lazy dog 1234567890-= !"$%^&*()_+ QWERTYUIOP{}ASDFGHJKL:@~|ZXCVBNM<><? /.,mnbvcxz\asdfghjkkl;'#][poiuytrewq789654123.0
    +-*/``""$%£ hello this is a test file using all the characters availible on the keyboard for input END END END END"

    so in the string it is:
    [0] = S, [1]=A, [2]=R ...ect along the text

    basically i need to convert each character in each cell of the 1d string array to its hesidecimal value .

    i have created my own method which will take in a char and return a string containing the charcters hex value.

        public static String toHex(char c)
        {
         char char2ascii = c;
         int i = 0;
         int num = (int) char2ascii;
         String hex ="";
        char[] digits = new char[2];
        do {
          digits[i++] = Character.forDigit(num % 16, 16);
          num /= 16;
        } while (num != 0);
     
        for (int j = 1; j >= 0; j--){
            hex += digits[j];
        }
        hex=hex.toUpperCase();
        return hex;
        }



    what i want to do is run each cell through the toHex method so i everntally have a string array containing the hex value of each character in my text.

    I understand this is long weinded but i hope u hunderstand what im trying to get at

    example

    i want:

    String[] hexarray = S, T, A, R, T

    a run it through my method to convert to hex
    then it will become

    String[] hexarray = 53, 54, 41, 52, 54


    does anyone know how i can achive this. Im not allowed to use inbuilt libarys and classes to do the hex conversion thats why i have my own method for it .


    Thank you


  2. #2
    Junior Member ZaneDarklace's Avatar
    Join Date
    Feb 2014
    Location
    Lawton, OK
    Posts
    15
    My Mood
    Lonely
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Char array to a String array with hex

    have you tried to use a loop? try inserting a loop and then use the code:

    System.out.println(Integer.toHexString(x));
    Rock On

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Char array to a String array with hex

    @ZaneDarklace, please check the dates on threads. This thread is over 3 years old - in internet time that is an eternity (what people call 'zombie threads'). Locking

Similar Threads

  1. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM
  2. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM