Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Can someone help me with decimal to char conversion

  • 29-10-2009 5:06pm
    #1
    Closed Accounts Posts: 3


    Hi Guys,

    I'm new to this forum and I was just wondering if perhaps someone could help me out with a problem I'm having. At the moment I am trying to read in decimal values using an input stream reader. Now the input that will eventually be coming in is a file of continuous hexadecimal content that i will convert to decimal and then convert into chars.(I can't find a straight conversion from Hex to Char, is there one?) But at the moment I am trying to get to grips with trying to convert the decimal word, "836577807669" into its char form Which is "SAMPLE". I have the following code that can take the decimal 83 and convert that to its char form of S.

    If i can explain my code clearer I have used a substring command on the inputted decimal characters to attempt to break it into set of two characters. It successfully get the first two characters and prints them out but it is after that in my loop that it just keeps repeating the first two characters in their decimal form and not moving on the the next two characters of the inputted decimal stream.


    public static void main(String[] args) throws FileNotFoundException, NumberFormatException, IOException
    {


    Hex2Decimal app = new Hex2Decimal();


    app.dectochar();

    }

    public void dectochar() throws IOException

    {


    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));


    System.out.println("Enter the Decimal number:");


    String str = buff.readLine();


    while(!str.isEmpty())


    {


    for(int i=0; i<str.length();i++)


    {


    String temp = str.substring(0, str.length()/(str.length()/2));


    // the above line takes the entire entered string in this case


    // 836577807669 and attempts to subdivide it into pairs of 2.


    int d = Integer.parseInt(temp);


    char c = (char) d ;


    // I think i need to be able to move onto the next two characters


    //here and i can't for the life of me figure it out. I know i need to


    //disregard the first two characters but i cant figure out how to move


    // onto them.


    System.

    out.println(c);

    }


    }

    }


    I'd really appreciate any help or enlightenment that anyone could offer.


Comments

  • Registered Users, Registered Users 2 Posts: 1,916 ✭✭✭ronivek


    Edited to remove stupid comment due to not reading the original post correctly!

    Secondly some of the logic you're using is incorrect; for example the while loop condition is giving you an infinite loop; I presume that's not what you want? In addition the way you're extracting the characters from your input String isn't going to work properly either; you're simply grabbing the first two characters every time.

    I'm assuming this is homework so I'm not going to give you a solution; but I would suggest trying to come up with a solution that doesn't involve substring and instead try thinking of the String as an array of characters and seeing if that might help you along.

    In terms of the Hexadecimal stuff again assuming this is homework then the intention may not be for you to use existing Java classes and instead come up with your own solution; in failing that then have a look through the Integer class and specifically at the parseInt function.


  • Closed Accounts Posts: 3 pro


    ronivek wrote: »
    Edited to remove stupid comment due to not reading the original post correctly!

    Secondly some of the logic you're using is incorrect; for example the while loop condition is giving you an infinite loop; I presume that's not what you want? In addition the way you're extracting the characters from your input String isn't going to work properly either; you're simply grabbing the first two characters every time.

    I'm assuming this is homework so I'm not going to give you a solution; but I would suggest trying to come up with a solution that doesn't involve substring and instead try thinking of the String as an array of characters and seeing if that might help you along.

    In terms of the Hexadecimal stuff again assuming this is homework then the intention may not be for you to use existing Java classes and instead come up with your own solution; in failing that then have a look through the Integer class and specifically at the parseInt function.

    Hi ronivek, thanks for your reply. The reason I am asking for help is because i am trying to extract some data from a file called index.dat, maybe you have heard of it. The file holds URL information from web browsers in Hexi-Decimal form and I would like to be able to pull it out and view it in plaintext. I'm confident that I can pull it out in hexadecimal to decimal form but i have hit a bump trying to convert from decimal then to char. I've looked into the Integer class and I've been searching around the java forums and i cant find a straight conversion technique so i'm looking to do it this way even if it is a bit round about.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    pro wrote: »
    Hi ronivek, thanks for your reply. The reason I am asking for help is because i am trying to extract some data from a file called index.dat, maybe you have heard of it. The file holds URL information from web browsers in Hexi-Decimal form and I would like to be able to pull it out and view it in plaintext. I'm confident that I can pull it out in hexadecimal to decimal form but i have hit a bump trying to convert from decimal then to char. I've looked into the Integer class and I've been searching around the java forums and i cant find a straight conversion technique so i'm looking to do it this way even if it is a bit round about.

    You obviously didnt search hard enough as the first page of results on google lists loads of sample code on java datatype conversions.

    Hex to Dec
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

    Dec to Char conversion just cast it


  • Closed Accounts Posts: 3 pro


    lynchie wrote: »
    You obviously didnt search hard enough as the first page of results on google lists loads of sample code on java datatype conversions.

    Hex to Dec
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

    Dec to Char conversion just cast it

    i swear to god i have 30 bookmarks of websites and that one never popped up. I must have been googling the wrong thing. Thanks for the redirect. :)


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    Just to point out, for your decimal-to-char conversion you take 2 digits each time... This isn't the best. The letter 'z' is 122 in decimal, 3 digits. Whereas 'a' is 97, just 2. Your program wouldn't work with letters past 'c'.

    What you'd want to do is seperate them (with spaces, commas or whatever) so you know when one char ends and other begins, as it would be fairly tricky to write a program that always gets it right (and impossible AFAIK if you want every character to be a valid input).
    This has extra benefit that if they're seperated like this you can get them from the input easier (most functions to get input will stopl, at any whitespace character, but readLine() doesn't) and simply converted them one at a time.


  • Advertisement
Advertisement