---
title: "判断字节数组是否为utf-8编码"
author: "floydd"
date: "2008-01-24T16:44:00Z"
tags: []
slug: "utf-8"
draft: false
lastmod: "2011-12-26T12:03:19Z"
id: 1326383
url: "https://floydd.iteye.com/blog/1326383"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)privatestaticvoidisValidUTF8(byte\[]bytes)throwsIllegalStateException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intcountToValidate=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(inti=0;i&lt;bytes.length&&countToValidate==0;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intb=bytes\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(b&gt;0)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)continue;//asciichar  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)debugInt(b,"cur");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(((b&gt;&gt;6)&0x3)==0x3)...{//checkthe11pattern  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)countToValidate++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)b=(b&lt;&lt;1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)debugInt(b,"cur\_c");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)i++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(i&gt;bytes.length-1)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnext=bytes\[i]&0xff;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)debugInt(next,"next");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if((next&gt;=0x80&&next&lt;0xc0))...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)countToValidate--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(countToValidate&gt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thrownewIllegalStateException();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

参考：  
http://blog.csdn.net/sandyen/archive/2006/08/23/1108168.aspx  
http://www.eygle.com/digest/2007/01/zhs16gbk\_char.html  
http://www.linux-cn.com/html/website/html/20070605/52875.html