Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Question Returning HexaDecimal Values to an Array???

    Hello all.I am Kishore.I am doing my B.Tech III year.
    I have the following doubt in Java.That's why i searched on google and i found this forum.For solution i registered here.I hope i get the solution.

    Problem:

    I have a String array containing some hex values like this..

    {620058,640055,6a0050,710048,740044}

    Now i have to send these values to a Integer Array such that the array receive the values like this...

    {0x620058,0x640055,0x6a0050,0x710048,0x740044}

    And it should print as it is.

    I think you understood my problem.
    How can i do it in java??
    Waiting for your reply..

    Thanks in advance..:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by stylishkishore View Post
    Now i have to send these values to a Integer Array such that the array receive the values like this...

    {0x620058,0x640055,0x6a0050,0x710048,0x740044}
    It should be easy enough to write a small method that iterates through the String array, tacks on "0x" to the front of each String, parses the String to int and places it into an int array that the method will return. Why not give it a shot?


    And it should print as it is.
    Huh?


    I think you understood my problem.
    Not that last statement. I have no idea how you want to print it and in what context.

  3. #3
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for giving reply.
    I mean, i have to send that Stiring Array to an Integer Array.Such that it includes 0x infront of those String values.
    And one more issue is,when we try to print that Integer Array it should print the values in Hex Format,but not in Integer Format.

    Let me explain clearley..

    Now let us think this is my String Array SA is

    {620058,640055,6a0050,710048,740044}

    What i want is..

    1. I just want to pass that SA to another Integer array IA such that it includes 0x for every element at the start.
    Like this...

    The resultant Integer Array IA should contain
    {0x620058,0x640055,0x6a0050,0x710048,0x740044}

    Now my second issue is..

    2. I just want to print that Integer array IA such that the output should be

    0x620058
    0x640055
    0x6a0050
    0x710048
    0x740044

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    1. To get an array of integer values, do as Fubarable suggested in #2: just parse the values one at a time. There is a method, parseInt(String,radix) that lets you do with without fluffing about with 0x

    2. To print a bunch of strings that look like the original strings but have "0x" prepended, do just that. Don't bother with the array of ints.

    Perhaps you think that integer values themselves have a radix: they don't. Although we speak of "hexadecimal integers", they aren't really a special sort of integer. Consider the number of eggs in a carton. You can't really say that this number is decimal or hex or anything else.

    The term hexadecimal really applies to numerals: that is to the strings that are used to denote integer values, print them, input them etc. So - referring to the same eggs - the strings "C" or "0xC" might be thought of as hexadecimal and contrasted with the decimal "12" and the radix-free "dozen".
    Last edited by pbrockway2; 12-17-2009 at 08:24 PM. Reason: carton was one off

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by stylishkishore View Post
    Thanks for giving reply.
    Now let us think this is my String Array SA is
    {620058,640055,6a0050,710048,740044}
    [nitpick]
    better:
    {"620058", "640055", "6a0050", "710048", "740044"}
    [/nitpick]
    ;)

    What i want is..
    1. I just want to pass that SA to another Integer array IA such that it includes 0x for every element at the start.
    Like this...

    The resultant Integer Array IA should contain
    {0x620058,0x640055,0x6a0050,0x710048,0x740044}
    as noted by pbrockway2, you will be should be able to pre-pend the "0x" to the strings and parse with Integer's parseInt(Strings, int radix) method.

    Now my second issue is..
    2. I just want to print that Integer array IA such that the output should be
    0x620058
    0x640055
    0x6a0050
    0x710048
    0x740044
    As per pbrockway2, the int array holds ints, not Strings, not String representatives of ints, but we can easily get the latter by using String.format(...) or Integer's toHexString(...) static method. You'll need to pre-pend the "0x" yourself, but everything else is done by the toHexString method.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    [ ... ] tacks on "0x" to the front of each String, parses the String to int [ ... ]
    The Integer.parseInt(String s, int radix) method doesn't take radix prefixes, i.e. the second argument determines the radix value so you can use those String values as they are.

    kind regards,

    Jos

Similar Threads

  1. Result set not returning values
    By karthikeyan_raju in forum Advanced Java
    Replies: 5
    Last Post: 04-14-2011, 05:24 AM
  2. problem on returning values..
    By kulangotski in forum Advanced Java
    Replies: 11
    Last Post: 03-07-2009, 02:13 AM
  3. Replies: 3
    Last Post: 12-10-2008, 01:38 AM
  4. Replies: 11
    Last Post: 12-08-2008, 12:29 AM
  5. Replies: 0
    Last Post: 11-30-2007, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •