Results 1 to 20 of 20
  1. #1
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Convertion char[] to double value

    hello guys ,
    please i need some help to understand the java Conversion.
    I have a char[] containing ASCII characters that need to be converted into int value and double value.

    The int value are allway stored in 1 char size like 'j'. I extracted it succesffully by converting the char in a ascii bytearray and then used: Integer.parseInt(sb.toString().replace("0x", ""), 16);


    My problem is how can i get the Value as double when i used the char[] with size 2 or 4 ?

    Example : final char[] charValue = { 'u', ' ', '}','+' }; what is the associate Double value ?
    Example : final char[] charValue = { 'T', ' ' }; what is the associate Double value ?

    Example : final char[] charValue = { 'T', ' ' }; what is the associate int value ?


    Thanks for you help

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Convertion char[] to double value

    This makes absolutely no sense to me. That first replacement you do

    Java Code:
    Integer.parseInt(sb.toString().replace("0x", ""), 16);
    The base 16 and the removal of the '0x' indicates to me that it is not just a char array with random ascii characters, it is in fact a hexadecimal string like 0xDEADBEEF. If it is a hexadecimal number in string form then you would have a combination of numbers and ascii characters A-F and nothing above F.

    Are you sure you understand your own homework assignment?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Re: Convertion char[] to double value

    Hi gimbal2
    thanks for you answer,
    the problem is following: I receive a Char[] containing ascii characters. In this char[] there are some values ( int and double) i want to get. So i need to convert some charaters into it int representation or double:

    To get the Int value i process like following

    1. convert the char[] into byteAscii :

    Java Code:
    public static byte[] charArrayToBytesArray_ASCII(char[] buffer) {
    		final byte[] b = new byte[buffer.length];
    		for (int i = 0; i < b.length; i++) {
    			b[i] = (byte) buffer[i];
    		}
    		return b;
    	}
    2. get the int value:

    Java Code:
    private static int charToInt(char[] data) {
    
    		final byte[] bytesArray_ASCII = charArrayToBytesArray_ASCII(data);
    		final StringBuilder sb = new StringBuilder();
    		for (final byte b : bytesArray_ASCII) {
    			sb.append(String.format("%02x", b & 0xff));
    		}
    		final int value = Integer.parseInt(sb.toString().replace("0x", ""), 16);
    		return value;
    
    	}
    Now i want to know how to convert a char[] with length 4 or 2 into it double value.
    eg: char[] charValue = { 'T', ' ', '3', 'k' };

    Thanks

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Convertion char[] to double value

    Where does the double come from? You can't convert 'T' into a double, unless you want the codepoint itself. You want to convert char '3' into double 3?

    Just loop the elements of the array. As long as you're using ASCII, the char should be in the range of 48 and 57 (both inclusive). Once you have a char in the said range substract 48 from it and you got your number: double three = '3' - 48;

    No need to go the wonky way with byte arrays, toString(), & 0xFF and what have you...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Convertion char[] to double value

    char should be in the range of 48 and 57
    The code would be more readable if the range was '0' t0 '9'

    double three = '3' - '0';
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Re: Convertion char[] to double value

    Quote Originally Posted by SurfMan View Post
    Where does the double come from? You can't convert 'T' into a double, unless you want the codepoint itself. You want to convert char '3' into double 3?

    Just loop the elements of the array. As long as you're using ASCII, the char should be in the range of 48 and 57 (both inclusive). Once you have a char in the said range substract 48 from it and you got your number: double three = '3' - 48;

    No need to go the wonky way with byte arrays, toString(), & 0xFF and what have you...
    The char[] i received contain int values ( corresponding to some code i my application) and also Double values (for longitude and latitude) . But all this value are stored as ascii character So i need for example to read 4 characters and convert into double value to get the longitude and the same for latitude . that's where the double come from.


    You say i need to loop the elements of the array ! after that i will get for each element an associated number. now how can it contruct the whole number based of this 4 number i got for each elements ?

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Convertion char[] to double value

    Quote Originally Posted by simplo View Post
    The char[] i received contain int values ( corresponding to some code i my application) and also Double values (for longitude and latitude) . But all this value are stored as ascii character So i need for example to read 4 characters and convert into double value to get the longitude and the same for latitude . that's where the double come from.


    You say i need to loop the elements of the array ! after that i will get for each element an associated number. now how can it contruct the whole number based of this 4 number i got for each elements ?
    I think it would be extremely helpful if you showed the char[] you are actually receiving, because there is no way a double can be in a char[]: { '3.1415927' }. It is just not going to happen.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Re: Convertion char[] to double value

    Quote Originally Posted by SurfMan View Post
    I think it would be extremely helpful if you showed the char[] you are actually receiving, because there is no way a double can be in a char[]: { '3.1415927' }. It is just not going to happen.
    You understand me wrong. here is the char[] i received :
    char[] = {' ','',' ','u',' ',' ',' ','u',' ',' ','','',' ','',' '}
    I read the first 4 char : char[] = {' ','',' ','u'} and want to convert it into double value (the longitude i need). Then the specification say the longitude value a stored in char[] with lenght 4.
    Last edited by simplo; 03-31-2015 at 02:22 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Convertion char[] to double value

    Can you give an example of a char array and the double value you want to convert it to?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Re: Convertion char[] to double value

    Hi Norm,
    for example : how can i convert this char[] = {' ','',' ','u'} into double value (the longitude i need). ?

    My general problem is: how to convert a char[] containing 4 ascii character into it Double representation (value) ?
    1. a double value are encode in char and stored into char[].
    2. i receive this char[] and want to decode it to get the double value.

    you can also check my last post

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convertion char[] to double value

    Are you certain the values are actually stored as characters and not binary data? From where are the values coming from? Are they read from a file?

    Regards,
    Jim
    Last edited by jim829; 03-31-2015 at 03:17 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Convertion char[] to double value

    a double value are encode in char and stored into char[].
    You need to explain how that is done. Also you need to post an example that gives a double and shows the char[] that it is converted to.
    For example (not real values): the double: 12.345 is converted to the char[]: 'f', '$', '#', 'h'

    Why are you working with a char[]? A byte[] would make more sense.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    20
    Rep Power
    0

    Default Re: Convertion char[] to double value

    Quote Originally Posted by jim829 View Post
    Are you certain the values are actually stored as characters and not binary data? From where are the values coming from? Are they read from a file?

    Regards,
    Jim
    yes all values are stored as characters. i don't know how this values are construct (the the i just get it from API) but the fact is: when i receive a char[] = {'T'} i can get the int value (84) this value can be cast as double.
    so now the Question are : its possible to get the double value from a char[] like this char[] = {' ','',' ','u'} ?? or char[] = {' ','T'} ??

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Convertion char[] to double value

    Quote Originally Posted by simplo View Post
    i don't know how this values are construct (the the i just get it from API)
    What API?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convertion char[] to double value

    What I think you have is a IEEE 754 encoded double, passed to you as a series of characters. You may want to check out the ByteBuffer class.
    You should also learn about internal representations of values. And please discuss this with your instructor or whomever provided the input.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Convertion char[] to double value

    i don't know how this values are construct
    This is the problem, I think. You don't know the way the numeric values are encoded into char arrays. And we certainly don't. So how can any of us figure out how to reverse the encoding?

    What Jim suggests sounds very hopeful.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Convertion char[] to double value

    Quote Originally Posted by pbrockway2 View Post
    What Jim suggests sounds very hopeful.
    Assuming (big assumption) that I know what the OP is talking about, if a double is broken up into 8 characters then it can be retrieved using ByteBuffer. I tried this and it worked just fine. I also used the String class as an intermediary to convert from bytes to chars and back again. I am curious though as to why the double is passed as a character array and how it became that way to begin with.

    Note: Using String to convert a byte array to char array resulted in a same size array (i.e. two bytes were not packed into a single 16 bit character). If the OP's char arrays were really 16 bit unsigned shorts and the high order byte was significant, then different logic would be required.

    Regards,
    Jim
    Last edited by jim829; 04-01-2015 at 12:50 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Convertion char[] to double value

    Quote Originally Posted by jim829 View Post
    Assuming (big assumption) that I know what the OP is talking about, if a double is broken up into 8 characters then it can be retrieved using ByteBuffer. I tried this and it worked just fine. I also used the String class as an intermediary to convert from bytes to chars and back again. I am curious though as to why the double is passed as a character array and how it became that way to begin with.
    Someone converted a C program they found and didn't actually think it through?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Convertion char[] to double value

    That's unfair, in a C program this would still be an odd way to do things :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Convertion char[] to double value

    Don't the C (or C++ at least) IO libs read chars?
    That was where I was heading...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. what after converting from char to double??
    By Jhaddoush in forum New To Java
    Replies: 10
    Last Post: 05-03-2013, 10:13 PM
  2. char to double
    By shred in forum New To Java
    Replies: 11
    Last Post: 09-25-2012, 11:44 PM
  3. Converting double to char array?
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 08-29-2010, 07:32 PM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. Program(convertion)
    By ayangupta in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-08-2009, 12:48 PM

Tags for this Thread

Posting Permissions

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