Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

get a String with its encoding value

coinscoins Posts: 13Member
I am having trouble to new a String with its encoding value given by an int type number. For example "yes", its encoding value in iso8859-1 is "79 65 73", so I want to get the String "yes" by giving the int number 79, 65 and 73.

I want to use the String Constructor below:
[code]
public String(byte[] bytes, String charsetName)
throws UnsupportedEncodingException
Parameters:
[red]bytes - the bytes to be decoded into characters[/red]
charsetName - the name of a supported charset
Throws:
UnsupportedEncodingException - If the named charset is not supported.
[/code]
My problem now is this method only receive the parameter in byte array,
what I have is int, I don't know how to convert the given value like "79" into byte array type for using this method to get the right String that I need.
[code]
import java.io.*;
import java.text.*;
import java.util.*;
import java.lang.*;


class int2byte
{
public static void main(String args[])
{

//int y = 79 ;
//int e = 65 ;
//int s = 73 ;
byte[] byary={79, 65, 73};
try{
String tempstr=new String(byary,"iso-8859-1");

System.out.println(tempstr);

}catch(UnsupportedEncodingException g){ System.out.println("Unsupported Encoding!");}


}
}
[/code]

Any thoughts will be appreciate.

Comments

  • zibadianzibadian Posts: 6,349Member
    : I am having trouble to new a String with its encoding value given by an int type number. For example "yes", its encoding value in iso8859-1 is "79 65 73", so I want to get the String "yes" by giving the int number 79, 65 and 73.
    :
    : I want to use the String Constructor below:
    : [code]
    : public String(byte[] bytes, String charsetName)
    : throws UnsupportedEncodingException
    : Parameters:
    : [red]bytes - the bytes to be decoded into characters[/red]
    : charsetName - the name of a supported charset
    : Throws:
    : UnsupportedEncodingException - If the named charset is not supported.
    : [/code]
    : My problem now is this method only receive the parameter in byte array,
    : what I have is int, I don't know how to convert the given value like "79" into byte array type for using this method to get the right String that I need.
    : [code]
    : import java.io.*;
    : import java.text.*;
    : import java.util.*;
    : import java.lang.*;
    :
    :
    : class int2byte
    : {
    : public static void main(String args[])
    : {
    :
    : //int y = 79 ;
    : //int e = 65 ;
    : //int s = 73 ;
    : byte[] byary={79, 65, 73};
    : try{
    : String tempstr=new String(byary,"iso-8859-1");
    :
    : System.out.println(tempstr);
    :
    : }catch(UnsupportedEncodingException g){ System.out.println("Unsupported Encoding!");}
    :
    :
    : }
    : }
    : [/code]
    :
    : Any thoughts will be appreciate.
    :
    :
    You can use the Integer class to get the byteValue() of each integer. See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html for more info
Sign In or Register to comment.