np.binary_repr输出不正确

时间:2018-07-06 09:23:06

标签: python string numpy binary ip

我的代码应该将IP从十进制转换为点分十进制,但是由于某些原因,np.binary_repr似乎无法正常工作。我尝试在Ipython控制台中更改值,但是如果我整体运行代码,它将开始产生错误的输出。

import numpy as np
print("Please enter your IP address:")
ip = input()
c = ip.split(".")
arr = np.zeros(4)

for i in range(len(c)):
    if c[i].startswith('0'):
        print("***********WARNING***********")
        print(c[i], "strted with '0', next time please avoid starting with '0'. We converted it for now, don't worry.")
arr[i] = int(c[i])
for i in range(4): 
    if arr[i] > 255 or arr[i] < 0:
    print("Please enter values between 0-255 only.")
    choice = '0'
    while(choice!='Y' and choice!='y' and choice!='N' and choice!='n'):
        print("Wrong choice! Please try again.")
        print("Do you wish to correct this number ",arr[i],"?(Y/N)")
        choice = input()
        print(choice=='Y')
        print(choice!='Y' or choice!='y' or choice!='N' or choice!='n')
    if choice == 'Y' or choice == 'y':
        print("Please enter the corrected value")
        arr[i] = input("Enter now:")
    elif choice!='N' or choice!='n': 
        print("Okay, restarting the process now. Good bye")
        break
arr = np.array(arr, dtype = np.int16)
print("Array before binary:", arr)
for i in range(4):
    print(arr[i])
    arr[i] = np.binary_repr(arr[i], width = 16)      
    print(arr[i])   

产生的输出是这个。

me@ccf128-OptiPlex-755:~$ python3 d2b.py
Please enter your IP address:
10.70.3.254
Array before binary: [ 10  70   3 254]
10
1010
70
17070
3
11
254
-30010
[  1010  17070     11 -30010]

如果有人可以指出我的错误,那太好了

0 个答案:

没有答案