(Java)문자열(String)을 쉬프트 하는 방법 (right shift) ,(left shift)
//////////////////RIGHT SHIFT
public static void main(String[] args) throws UnsupportedEncodingException{
shift("010000",3);
}
public final static void shift(String str,int size)
{
byte[] orgByte=null;
byte[] resultByte=new byte[str.length()];
orgByte=str.getBytes();
int strlen=orgByte.length;
for(int i=0;i<strlen;i++){
resultByte[(i+size)%strlen]=orgByte[i];
}
String result=null;
result=new String(resultByte);
System.out.println(result);
}
//LEFT SHFIT
public final static void left_shift(String str,int size) throws UnsupportedEncodingException{
byte[] orgByte=null;
byte[] resultByte=new byte[str.length()];
orgByte=str.getBytes("ASCII");
String s="0";
byte []a=s.getBytes("ASCII");
int strlen=orgByte.length;
for(int i=1;i<strlen;i++){
resultByte[(i-size)%strlen]=orgByte[i];
if(i==strlen-1){
resultByte[i]=a[0];
}
}
String result=null;
result=new String(resultByte);
System.out.println(result);
}
다음 사이트를 참고했습니다.
http://beonit2.tistory.com/entry/Java-string-right-shift
public static void main(String[] args) throws UnsupportedEncodingException{
shift("010000",3);
}
public final static void shift(String str,int size)
{
byte[] orgByte=null;
byte[] resultByte=new byte[str.length()];
orgByte=str.getBytes();
int strlen=orgByte.length;
for(int i=0;i<strlen;i++){
resultByte[(i+size)%strlen]=orgByte[i];
}
String result=null;
result=new String(resultByte);
System.out.println(result);
}
//LEFT SHFIT
public final static void left_shift(String str,int size) throws UnsupportedEncodingException{
byte[] orgByte=null;
byte[] resultByte=new byte[str.length()];
orgByte=str.getBytes("ASCII");
String s="0";
byte []a=s.getBytes("ASCII");
int strlen=orgByte.length;
for(int i=1;i<strlen;i++){
resultByte[(i-size)%strlen]=orgByte[i];
if(i==strlen-1){
resultByte[i]=a[0];
}
}
String result=null;
result=new String(resultByte);
System.out.println(result);
}
다음 사이트를 참고했습니다.
http://beonit2.tistory.com/entry/Java-string-right-shift
댓글
댓글 쓰기