第一题:每个汉字对应两个字节,而每个字节的首位都是1,这样对应的就是一个负数.class SplitString
{
private String str;
private int byteNum; public SplitString(){} public SplitString(String str,int byteNum)
{
this.str=str;
this.byteNum=byteNum;
}
public void splitIt()
{ byte bt[]=str.getBytes();
System.out.println("Length of this String ===>"+bt.length);
if(byteNum>1)
{
if(bt[byteNum]<0)
{
pBinInt("bt[byteNum]",bt[byteNum]);
System.out.println("bt["+byteNum+"] = "+bt[byteNum]);//1
System.out.println("bt["+byteNum+"] = "+(int)bt[byteNum]);//2
System.out.println("bt["+byteNum+"] = "+(bt[byteNum]&0x000000FF));//3
System.out.println("bt["+byteNum+"] = "+(bt[byteNum+1]&0XFF));//4
String substrx=new String(bt,0,--byteNum);
System.out.println(substrx);
}
else
{
String substrex=new String(bt,0,byteNum);
System.out.println(substrex);
}
}
else
{
if(byteNum==1)
{
if(bt[byteNum]<0)
{
String substr1=new String(bt,0,++byteNum);
System.out.println(substr1);
}
else
{
String subStr2=new String(bt,0,byteNum);
System.out.println(subStr2);
}
}
else
{
System.out.println("输入错误!!!请输入大于零的整数:");
}
}
}
static void pBinInt(String s, int i) {
System.out.println(
s + ", int: " + i + ", binary: ");
System.out.print(" ");
for(int j = 31; j >=0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
}
class TestSplitString
{
public static void main(String args[])
{
String str="我ABC汉DEF";
int num=6;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
}
------------------------------------------------第二题:随便找一本java关于网络编程的都会有这个例子的,楼主自己找找看吧
{
private String str;
private int byteNum; public SplitString(){} public SplitString(String str,int byteNum)
{
this.str=str;
this.byteNum=byteNum;
}
public void splitIt()
{ byte bt[]=str.getBytes();
System.out.println("Length of this String ===>"+bt.length);
if(byteNum>1)
{
if(bt[byteNum]<0)
{
pBinInt("bt[byteNum]",bt[byteNum]);
System.out.println("bt["+byteNum+"] = "+bt[byteNum]);//1
System.out.println("bt["+byteNum+"] = "+(int)bt[byteNum]);//2
System.out.println("bt["+byteNum+"] = "+(bt[byteNum]&0x000000FF));//3
System.out.println("bt["+byteNum+"] = "+(bt[byteNum+1]&0XFF));//4
String substrx=new String(bt,0,--byteNum);
System.out.println(substrx);
}
else
{
String substrex=new String(bt,0,byteNum);
System.out.println(substrex);
}
}
else
{
if(byteNum==1)
{
if(bt[byteNum]<0)
{
String substr1=new String(bt,0,++byteNum);
System.out.println(substr1);
}
else
{
String subStr2=new String(bt,0,byteNum);
System.out.println(subStr2);
}
}
else
{
System.out.println("输入错误!!!请输入大于零的整数:");
}
}
}
static void pBinInt(String s, int i) {
System.out.println(
s + ", int: " + i + ", binary: ");
System.out.print(" ");
for(int j = 31; j >=0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
}
class TestSplitString
{
public static void main(String args[])
{
String str="我ABC汉DEF";
int num=6;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
}
------------------------------------------------第二题:随便找一本java关于网络编程的都会有这个例子的,楼主自己找找看吧
{
public static void main (String [] args)
{
cutstring ct = new cutstring();
String str= ct.getStr("我ABC汉DEF",6);
System.out.println(str);
}
public static String getStr(String src, int len) {
if (src == null) return null;
if (src.getBytes().length <= len) return src;
byte[] s = src.getBytes();
int flag = 0;
for(int i=0;i<len;i++){
if(s[i] < 0) flag++;
}
if(flag%2!=0) len--;
byte[] d = new byte[len];
System.arraycopy(s, 0, d, 0, len);
return new String(d);
}
}
第二题誰给写出来,我现在这里没有书呀!不好意思,不要BS我!谢谢!
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;public class ClientSocket{
private String sendMsg = "" ;
private String receiveMsg = "" ;
public void connection(String ipAddress, int port){
try{
//build connection
Socket socket = new Socket(ipAddress,port);
System.out.println( "The client has connected!" + "\n" ) ;
while(true){
//send message
int ch ;
this.sendMsg = "" ;
while(true){
ch = System.in.read();
this.sendMsg += (char) ch ;
if( (char)ch =='\n')
break;
}
OutputStream out = socket.getOutputStream();
out.write(this.sendMsg.getBytes());
out.flush();
System.out.println("The client has sended the message" + "\n");
//receive message
InputStream in = socket.getInputStream();
this.receiveMsg = "";
int ch2;
while ( true ){
ch2 = in.read();
this.receiveMsg +=(char)ch2 ;
if( (char)ch2 == '\r' )
break;
}
System.out.print("Received from the server :" );
System.out.println(this.receiveMsg);
System.out.println();
}
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
ClientSocket csocket = new ClientSocket();
csocket.connection("172.18.5.172",8000);
}
}
----------------------------------------------------------------------
服务器端:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;public class ServerSocketIns{
private String receiveMsg = "";
private String sendMsg = "";
public void doListener(int port){
try{
//build connection
ServerSocket server = new ServerSocket(port);
Socket socket = server.accept();
System.out.println( "The server is listenning!" + "\n" ) ;
while(true){
//receive message
InputStream in = socket.getInputStream();
this.receiveMsg = "" ;
int ch ;
while (true){
ch = in.read();
if ((char)ch == '\r')
break;
else
this.receiveMsg += (char) ch;
}
System.out.println("Receive from The Client :" + this.receiveMsg + "\n" );
this.sendMsg = this.receiveMsg ;
//send message
OutputStream out = socket.getOutputStream();
this.sendMsg += " at Time: " ;
this.sendMsg += (new java.util.Date()).toString();
this.sendMsg += '\r' ;
out.write( this.sendMsg.getBytes() );
out.flush();
System.out.println("The server has resended the message back!" );
System.out.println(this.sendMsg);
System.out.println();
this.sendMsg = "" ;
}
}
catch(IOException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
ServerSocketIns ssocket = new ServerSocketIns() ;
ssocket.doListener( 8000 ) ;
}
}