那天情人节,我给她发了一串数字(01001001 00100000 01101100 01101111 01110110 01100101 00100000 01111001 01101111 01110101),她说我不懂浪漫,我低头苦笑,其实她不知道,这是二进制形式的"I love you",难道程序员真的注定孤独一生?
程序写得比较粗陋,作为不登大雅之堂的娱乐工具,足够了。
public class MonkeyRomatic {
public static void main(String args[]){
String wordsString = "I love you";
char[] wordsArray = wordsString.toCharArray();//将要表白的语句转为字符数组
int tempWord;//用于临时存放每个字符的ASCII码
int tempWordLength = 0;
String tempWords = "";//用于临时存放每个字符的二进制表示形式
String codeWords = "";//编码之后的表白语句
for(int i = 0;i < wordsArray.length; i++){
tempWord = wordsArray[i];//获取当前字符的ASCII码
tempWords = Integer.toBinaryString(tempWord);
tempWordLength = tempWords.length();
if(tempWordLength < 8){
//为美观起见,每个字符的二进制表示形式均限制为8位
for(int j = 0;j< 8 - tempWordLength;j++){
tempWords = "0" + tempWords;
}
}
codeWords = codeWords + tempWords+ " ";
tempWords = "";//将当前二进制字符归空
}
System.out.println(codeWords);
}
}
输出结果:
01001001 00100000 01101100 01101111 01110110 01100101 00100000 01111001 01101111 01110101
这就是二进制形式的"I love you"。
情人节当天,作者通过将情话'I love you'转换成二进制形式来表达浪漫,尽管代码实现较为简单粗糙,但背后蕴含的情感却别具一格。本文不仅展示了编程技巧,也探讨了程序员表达爱意的独特方式。

6万+

被折叠的 条评论
为什么被折叠?



