Java String Encoding - Javatpoint

文章推薦指數: 80 %
投票人數:10人

In Java, when we deal with String sometimes it is required to encode a string in a specific character set. Encoding is a way to convert data from one format to ... ⇧SCROLLTOTOP Home Java Programs OOPs String Exception Multithreading Collections JavaFX JSP Spring SpringBoot Projects InterviewQuestions JavaTutorial WhatisJava HistoryofJava FeaturesofJava C++vsJava HelloJavaProgram ProgramInternal Howtosetpath? JDK,JREandJVM JVM:JavaVirtualMachine JavaVariables JavaDataTypes UnicodeSystem Operators Keywords ControlStatements JavaControlStatements JavaIf-else JavaSwitch JavaForLoop JavaWhileLoop JavaDoWhileLoop JavaBreak JavaContinue JavaComments JavaPrograms JavaObjectClass JavaOOPsConcepts NamingConvention ObjectandClass Method Constructor statickeyword thiskeyword JavaInheritance Inheritance(IS-A) Aggregation(HAS-A) JavaPolymorphism MethodOverloading MethodOverriding CovariantReturnType superkeyword InstanceInitializerblock finalkeyword RuntimePolymorphism DynamicBinding instanceofoperator JavaAbstraction Abstractclass Interface AbstractvsInterface JavaEncapsulation Package AccessModifiers Encapsulation JavaArray JavaArray JavaOOPsMisc Objectclass ObjectCloning Mathclass WrapperClass JavaRecursion CallByValue strictfpkeyword javadoctool CommandLineArg ObjectvsClass OverloadingvsOverriding JavaString JavaRegex ExceptionHandling JavaInnerclasses JavaMultithreading JavaI/O JavaNetworking JavaAWT&Events JavaSwing JavaFX JavaApplet JavaReflection JavaDate JavaConversion JavaCollection JavaJDBC JavaMisc JavaNewFeatures RMI Internationalization InterviewQuestions JavaMCQ next→ ←prev JavaStringEncoding InJava,whenwedealwithStringsometimesitisrequiredtoencodeastringinaspecificcharacterset.Encodingisawaytoconvertdatafromoneformattoanother.StringobjectsuseUTF-16encoding.TheproblemwithUTF-16isthatitcannotbemodified.Thereisonlyonewaythatcanbeusedtogetdifferentencodingi.e.byte[]array.Thewayofencodingisnotsuitableifwegetunexpecteddata.Inthissection,wewilllearnhowtoencodeastringinJava. Note:ItisnotpossibletoencodeastringinUTF-8.So,useeitherByteBufferorcallanarrayonittogetabyte[]. Beforemovingaheadinthissection,wehavetounderstandcharacterencoding.Let'shaveaquicklook.Let'sunderstandwhyweneedtoencodeastring. Characterencodingisatechniquetoconverttextdataintobinarynumbers.Wecanassignuniquenumericvaluestospecificcharactersandconvertthosenumbersintobinarylanguage.Thesebinarynumberslatercanbeconvertedbacktooriginalcharactersbasedontheirvalues. Problem Suppose,wehaveGermanstringTschüssanditisrequiredtoencodeit.Considerthefollowingcodesnippet: Stringstr="Tschüss"; byte[]bytesOfString=str.getBytes(); Stringencoded_String=newString(bytesOfString,StandardCharsets.US_ASCII);assertNotEquals(encoded_String,str); IfweencodethestringbyusingtheUS_ASCII,itgivestheTsch?ssbecausetheUS_ASCIIencodingdoesnotunderstandthenon-ASCIIcharacter(ü).WhenweconvertanASCIIencodedstringtoUTF-8,wegetthesamestring. Stringstr="GoodBye"; byte[]bytesOfString=str.getBytes(); StringasciiEncondedEnglishString=newString(bytesOfString,StandardCharsets.US_ASCII); assertEquals(encoded_String,str); Ifabyte[]arraycontainsnon-Unicodetext,wecanconvertthetextintoUnicodewithStringconstructor.Conversely,wecanalsoconvertaStringobjectintoabyte[]arrayofnon-UnicodecharacterswiththeString.getBytes()method.Let'sencodethestringbyusingthegetBytes()method. UsingString.getBytes()Method JavaStringclassprovidesthegetBytes()methodthatisusedtoencodesstringintoUTF-8.Themethodconvertsthestringintoasequenceofbytesandstorestheresultintoanarray. Syntax: publicbyte[]getBytes(StringcharsetName)throwsUnsupportedEncodingException ItparsescharsetNameasaparameterandreturnsthebytearray.ItthrowstheUnsupportedEncodingExceptionifthenamedcharsetisnotsupported. Let'screateaJavaprogramthatconvertsastringintoUTF-8encoding. StringEncodingExample.java publicclassStringEncodingExample { publicstaticvoidmain(Stringargs[])throwsException { //stringtoencode Stringstr="GoogleCloud"; //invokesthegetBytes()methodandstoresanarrayofbytesintoarray[] bytearray[]=str.getBytes("UTF8"); System.out.println("EncodedString:"); //enhancedforloopthatiteratesoverthearray for(bytex:array) { //printsthesequenceofbytes System.out.print(x+""); } } } Output: EncodedString: 711111111031081013267108111117100 UsingStandardCharsetsClass WecanalsousetheStandardCharsetclasstoencodethestring.Therearetwostepstoencodethestring.First,decodethestringintobytesandthenencodeitintoUTF-8.Forexample,considerthefollowingcode: Stringstr="Tschüss"; ByteBufferbuffer=StandardCharsets.UTF_8.encode(str); Stringencoded_String=StandardCharsets.UTF_8.decode(buffer).toString();assertEquals(str,encoded_String); AnotherwaytoencodeastringistousetheBase64encoding.WewilldiscusstheBase64encodinganddecodinginthecomingsection. NextTopicPrim'salgorithmJava ←prev next→ ForVideosJoinOurYoutubeChannel:JoinNow Feedback SendyourFeedbackto[email protected] HelpOthers,PleaseShare LearnLatestTutorials Splunk SPSS Swagger Transact-SQL Tumblr ReactJS Regex ReinforcementLearning RProgramming RxJS ReactNative PythonDesignPatterns PythonPillow PythonTurtle Keras Preparation Aptitude Reasoning VerbalAbility InterviewQuestions CompanyQuestions TrendingTechnologies ArtificialIntelligence AWS Selenium CloudComputing Hadoop ReactJS DataScience Angular7 Blockchain Git MachineLearning DevOps B.Tech/MCA DBMS DataStructures DAA OperatingSystem ComputerNetwork CompilerDesign ComputerOrganization DiscreteMathematics EthicalHacking ComputerGraphics SoftwareEngineering WebTechnology CyberSecurity Automata CProgramming C++ Java .Net Python Programs ControlSystem DataMining DataWarehouse JavatpointServicesJavaTpointofferstoomanyhighqualityservices.Mailuson[email protected],togetmoreinformationaboutgivenservices.WebsiteDesigningWebsiteDevelopmentJavaDevelopmentPHPDevelopmentWordPressGraphicDesigningLogoDigitalMarketingOnPageandOffPageSEOPPCContentDevelopmentCorporateTrainingClassroomandOnlineTrainingDataEntryTrainingForCollegeCampusJavaTpointofferscollegecampustrainingonCoreJava,AdvanceJava,.Net,Android,Hadoop,PHP,WebTechnologyandPython.Pleasemailyourrequirementat[email protected]Duration:1weekto2weekLike/Subscribeusforlatestupdatesornewsletter



請為這篇文章評分?