Convert String to UTF-8 bytes in Java - Tutorialspoint
文章推薦指數: 80 %
UTF-8 is a variable width character encoding. UTF-8 has ability to be as condense as ASCII but can also contain any unicode characters with some ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho ConvertStringtoUTF-8bytesinJava Java8ObjectOrientedProgrammingProgramming CompleteJavaProgrammingFundamentalsWithSampleProjects 98Lectures 7.5hours EmenwaGlobal,EjikeIfeanyiChukwu MoreDetail GetyourJavadreamjob!Beginnersinterviewpreparation 85Lectures 6hours YuvalIshay MoreDetail CoreJavabootcampprogramwithHandsonpractice Featured 99Lectures 17hours PrashantMishra MoreDetail BeforeconvertingaStringtoUTF-8-bytes,letushavealookatUTF-8.UTF-8isavariablewidthcharacterencoding.UTF-8hasabilitytobeascondenseasASCIIbutcanalsocontainanyunicodecharacterswithsomeincreaseinthesizeofthefile.UTFstandsforUnicodeTransformationFormat.The'8'signifiesthatitallocates8-bitblockstodenoteacharacter.Thenumberofblocksneededtorepresentacharactervariesfrom1to4.InordertoconvertaStringintoUTF-8,weusethegetBytes()methodinJava.ThegetBytes()methodencodesaStringintoasequenceofbytesandreturnsabytearray.Declaration−ThegetBytes()methodisdeclaredasfollows−publicbyte[]getBytes(StringcharsetName)wherecharsetNameisthespecificcharsetbywhichtheStringisencodedintoanarrayofbytes.LetusseeaprogramtoconvertaStringintoUTF-8bytesinJava.Example LiveDemopublicclassExample{ publicstaticvoidmain(Stringargs[])throwsException{ Strings="HelloWorld"; bytearr[]=s.getBytes("UTF8"); for(bytex:arr){ System.out.print(x+""); } } }Output721011081081113287111114108100Letusunderstandtheaboveprogram.WehavecreatedaStrings−Strings="HelloWorld";StringsisassignedthevalueHelloWorld.ToconvertitintoUTF-8,weusethegetBytes(“UTF-8”)method.Thisgivesusanarrayofbytesasfollows−byte[]arr=s.getBytes("UTF-8");Thentoprintthebytearray,weuseanenhancedforloopasfollows−for(bytex:arr){ System.out.print(x+""); } AnviJain Updatedon26-Jun-202013:45:42 RelatedQuestions&AnswersConvertUnicodetoUTF-8inJava ConvertUTF-8toUnicodeinJava ConvertASCIITOUTF-8EncodinginPHP? Convertbytestoastringinjava HowtoconvertwronglyencodeddatatoUTF-8inMySQL? UTF-8ValidationinC++ HowtoconvertanMySQLdatabasecharactersetandcollationtoUTF-8? HowtoconvertDatetoStringinJava8? HowmanybitsareusedtorepresentUnicode,ASCII,UTF-16,andUTF-8charactersinjava? HowcanIconvertbytestoaPythonstring? HowwouldyouconvertstringtobytesinPython3? ChangeMySQLdefaultcharactersettoUTF-8inmy.cnf? Howtoreadandwriteunicode(UTF-8)filesinPython? HowcanTensorflowtextbeusedtosplittheUTF-8stringsinPython? HowtoConvertaJava8StreamtoanArray? PreviousPage PrintPage NextPage Advertisements
延伸文章資訊
- 1Charset (Java SE 16 & JDK 16) - Oracle Help Center
- 2Encode String in UTF-8 in Java | Delft Stack
Encode String in UTF-8 in Java · Encode a String to UTF-8 by Converting It to Bytes Array and Usi...
- 3Encode a String to UTF-8 in Java - Stack Abuse
Encoding a String in Java simply means injecting certain bytes into the byte array that constitut...
- 4Encode a String to UTF-8 in Java - Baeldung
Strings are immutable in Java, which means we cannot change a String character encoding. To achie...
- 5Java 的字串
任何一本Java 入門的書都會談到,Java 的字串使用Unicode,那麼是否想過,明明你的文字編輯器是使用MS950 編碼,為什麼會寫下的字串在JVM 中會是Unicode?如果在一個.