IPv6アドレスをバイナリ文字列に変換するにはどうすればよいですか?IPv6アドレスをバイナリ文字列に変換するにはどうすればよいですか?
例:
IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Binary: 0010000000000001 0000110110111000 1000010110100011 0000000000000000 0000000000000000 1000101000101110 0000001101110000 0111001100110100
私はJavaでこれをやりたいです。
C++で実装され、私はもっとよく知って:
#include <iostream>
#include <string>
#define _BSD_SOURCE
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
std::string toBinary(unsigned long int decimalIpV6)
{
std::string r;
while(decimalIpV6!=0) {r=(decimalIpV6%2==0 ?"0":"1")+r; decimalIpV6/=2;}
return r;
}
unsigned long int ipV6toDecimal(std::string binaryString) {
struct sockaddr_in antelope;
inet_aton(binaryString.c_str(), &antelope.sin_addr); // store IP in antelope
// and this call is the same as the inet_aton() call, above:
antelope.sin_addr.s_addr = inet_addr(binaryString.c_str());
return antelope.sin_addr.s_addr;
}
int main() {
std::string ipv6 = "192.168.0.0";
unsigned long int ipv6decimal= ipV6toDecimal(ipv6);
std::cout << toBinary(ipv6decimal) << std::endl;
return 0;
}
バグがあると(間違った結果を生成するここに私の失敗した試みである(私は全くこの試みに関連するソリューションを要求しません) "1010100011000000")。
PS:IPv6をテストするときにあなたを助けるかもしれないバイナリ電卓onlineにあります。
'私はJavaでこれをやりたいここに私の失敗した試行 '...うーん... – Henrik
ええ@Henrik、それは私が最初にそれを試していない場合は悪いだろう! – gsamaras
もちろん、それは精神ですが、それは含まれていないようです。 :)あなたの作業C++バージョンのみ。 – Henrik