本文主要是介绍将七进制字符串转换为int整数(整数转换为七进制),将字符按顺序排列,输出加密后的6为验证码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
将七进制字符串转换为int整数
public static void main(String[] args) {// 被转换的数字 基数int num=Integer.parseInt("123",7);System.out.println(num);// System.out.println((int)'0');
// System.out.println((int)'1');
// System.out.println((int)'2');
// System.out.println((int)'3');
// System.out.println('3'-'0');num=parseInt("-123",7);System.out.println(num);}public static int parseInt(String number,int radix){int end=0;int s=1;if (number.charAt(0)=='-'){s=-1;end=1;}int weight=1;int sum=0;for (int i=number.length()-1;i>=end;i--){char c=number.charAt(i);int n=c-'0';if (n<0||n>=radix){throw new NumberFormatException("数字错误"+c);}sum += n * weight;weight *= radix;}return s==-1? -sum:sum;//这里也能用乘法,不过乘法的性能慢}
将整数转换为其他进制
public class Demo02 {public static void main(String[] args) {String str=Integer.toString(66,7);System.out.println(str);str=toString(-166,16);System.out.println(str);}static final char[] digi={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};public static String toString(int num,int radix){boolean negative=false;if (num<0){negative=true;num=-num;}char[] chars=new char[32];int index=chars.length-1;do {int last=num % radix;char c=digi[last];chars[index--]=c;num /=radix;}while (num!=0);if (negative){chars[index--]='-';}return new String(chars,index+1,chars.length-index-1);}
}
将字符串安A-Z计算出数量
package cn.tedu.test;public class Demo03 {public static void main(String[] args) {System.out.println((int)'a');System.out.println((int)'z');System.out.println((int)'A');System.out.println((int)'Z');int[] counter=new int[26];String str="你好QWERTYUIOPAAABBBSDFGHJKLZXCVBNMZZZZZZ";for (int i=0;i<str.length();i++){char c=str.charAt(i);if (c>='A' && c<='Z'){counter[c-'A']++;}}for(char c='A';c<='Z';c++){System.out.println(c+":"+counter[c-'A']);}}}
将字符串安A-Z计算出数量 并且按照从大到小的数量排列
package cn.tedu.test;import java.util.Arrays;public class Demo04 {public static void main(String[] args) {/* int[][] counter={{'A',0},{'B',0},{'C',0},{'D',0},{'E',0},{'F',0},{'G',0},{'H',0},{'I',0},{'G',0},{'K',0},{'L',0},{'M',0},{'N',0},{'O',0},{'P',0},{'Q',0},{'I',0},{'S',0},{'T',0},{'U',0},{'V',0},{'W',0},{'X',0},{'Y',0},{'Z',0}};*/int [][] counter=new int [26][2];for (char c='A';c<='Z';c++){counter[c-'A'][0]=c;}String str="你好QWERTYUIOPAAABBBSDFFHGJKJHGDKMVGHJKLZXCVBNMZZZZZZ";for (int i=0;i<str.length();i++){char c=str.charAt(i);if (c>='A' && c<='Z'){counter[c-'A'][1]++;}}Arrays.sort(counter,(arr1,arr2)->arr2[1]-arr1[1]);for(int[] arr:counter){System.out.println((char)arr[0]+":"+arr[1]);}}}
随机生成六位数,并对算法加密,小写变大写,大写变小写,数字复制n+1次.
package cn.tedu.test;public class test {//随机生成六位数,并对算法加密,小写变大写,大写变小写,数字复制n+1次.public static void main(String[] args) {String m1="QWERTYUIOPASDFGHJKLZXCCVVBNMqwertyuioplmkjnhbgvfcdxsza0123456789";String m2="";StringBuilder buf=new StringBuilder();for (int a=0;a<6;a++){int num = (int) ((Math.random() * 62));String a1 = String.valueOf(m1.charAt(num));System.out.println("输入的字符:"+a1);if (a1.matches("[a-z]")){buf.append(a1.toUpperCase());}else if(a1.matches("[A-Z]")){buf.append(a1.toLowerCase());}else if (a1.matches("[0-9]")){int a2=Integer.parseInt(a1);for (int i = 0; i <a2+1 ; i++) {buf.append(a1);}}String a3=buf.toString();System.out.println("密码为:"+a3);}}
}
public class Demo07 {public static void main(String[] args) {//生产6个字符String chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz"+"0123456789-_";char[] code = new char[6];Random r = new Random();for(int i=0; i<code.length; i++){code[i] = chs.charAt(r.nextInt(chs.length()));}System.out.println(code);//进行编码StringBuilder buf = new StringBuilder();for (char c:code) {if(c>='A' && c<='Z'){buf.append((char)(c+32));}else if(c>='a' && c<='z'){buf.append((char)(c-32));}else if(c>='0' && c<='9'){int n = c - '0';for(int i=0; i<=n; i++){buf.append(c);}}else{buf.append("\\").append(c=='-'?'_':'-');}}String encode = buf.toString();System.out.println(encode);
}}
这篇关于将七进制字符串转换为int整数(整数转换为七进制),将字符按顺序排列,输出加密后的6为验证码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!