将新型冠状病毒转二进制的代码(首发)
编程学习 2021-07-05 13:20www.dzhlxh.cn编程入门
这篇文章主要介绍了新型冠状病毒转二进制的相关知识,分为java,js,php,pthon等语言的实例代码,需要的朋友可以参考下
视频
将“新型冠状病毒”转换为二进制???
视频地址:
Java
public class Main { public static void main(String[] args) { String str = toBinary("新型冠状病毒"); System.out.println(str); } private static String toBinary(String str) { char[] strChar = str.toCharArray(); StringBuilder result = new StringBuilder(); for (char c : strChar) { result.append(Integer.toBinaryString(c)); } return result.toString(); } }
JavaScript
function strToBinary(str){ var result = []; var list = str.split(""); for(var i=0;i<list.length;i++){ var item = list[i]; var binaryStr = item.charCodeAt().toString(2); result.push(binartStr); } return result.join(""); } console.log(strToBinary("新型冠状病毒"));
Php
function StrToBin($str){ $arr = preg_split('/(?<!^)(?!$)/u', $str); foreach($arr as &$v){ $temp = unpack('H*', $v); $v = base_convert($temp[1], 16, 2); unset($temp); } return join(' ',$arr); } echo StrToBin("新型冠状病毒");
Python
def encode(s): return ' '.join([bin(ord(c)).replace('0b', '') for c in s]) def decode(s): return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]]) var1 = encode("新型冠状病毒") print(var1) print(decode(var1)) var1Arr = var1.split(" ") for i in range(len(var1Arr)): print(var1Arr[i])
C++
#include <iostream> #include <stdlib.h> using namespace std; void outc(char c) { unsigned char k = 0x80 for (int i=0; i<8; i++, k >>= 1){ if (c & k){ printf("1"); } else{ printf("0"); } } printf(" "); } int main (){ char str[]="新型冠状病毒"; for(int i=0; str[i]; i++){ outc(str[i]); } system("pause"); return true;
总结
以上所述是长沙网络推广给大家介绍的将新型冠状病毒转二进制的代码,希望对大家有所帮助!
上一篇:解决VIM显示utf-8文件乱码问题
下一篇:Webpack基础教程之名词解释