记一次网络实训


记一次网络实训

这次网络实训是使用python去完成的

零比特填充

def bit():
    num = input('请输入您要填充的标志字段')
    list=0
    list_result = []
    # add=''
    # index=0
    for i in num:
        # index+=1
        if i == '1':
            if list >=5:
                list=0
                list_result.append('0')
                list_result.append('1')
                list+=1
            else:
                list+=1
                # add+=str(index)+'    '+str(i)+'   '
                list_result.append('1')
        else:
            list=0
            list_result.append(i)
    # print("add",add)
    result=''
    for item in list_result:
        result+=item
    print(result)

这个就是零比特填充
在HDLC的帧结构中,若在两个标志字段之间的比特串中,碰巧出现了和标志字段F(01111110)一样的比特组合,那么就会误认为是帧的边界。为了避免出现这种情况,HDLC采用零比特填充法使一帧中两个F字段之间不会出现6个连续1

海明码确认与创建

海明码确认

def sure_haiming():
    # for index in range(get_hiaming_id(num)):
        #print(type(index))
        #result+=return_Gn(num,index)*2**(index-1)
    num=input("请输入您要验证的海明码")
    result=0
    if get_hiaming_id(num)==5:
        result=return_Gn(num,5)*2**4+return_Gn(num,4)*2**3+return_Gn(num,3)*2**2+return_Gn(num,2)*2+return_Gn(num,1)
    elif get_hiaming_id(num)==4:
        result =return_Gn(num, 4) * 2 ** 3 + return_Gn(num, 3) * 2 ** 2 + return_Gn(num,2) * 2 + return_Gn(num, 1)
    elif get_hiaming_id(num) == 3:
        result = return_Gn(num, 3) * 2 ** 2 + return_Gn(num, 2) * 2 + return_Gn(num, 1)
    elif get_hiaming_id(num)==2:
        result =return_Gn(num,2) * 2 + return_Gn(num, 1)
    elif get_hiaming_id(num)==1:
        result =return_Gn(num, 1)
    elif get_hiaming_id(num)==6:
        result=return_Gn(num,6)*2**5+return_Gn(num,5)*2**4+return_Gn(num,4)*2**3+return_Gn(num,3)*2**2+return_Gn(num,2)*2+return_Gn(num,1)
    elif get_hiaming_id(num)==7:
        result=return_Gn(num,7)*2**6+return_Gn(num,6)*2**5+return_Gn(num,5)*2**4+return_Gn(num,4)*2**3+return_Gn(num,3)*2**2+return_Gn(num,2)*2+return_Gn(num,1)
    elif get_hiaming_id(num)==8:
        result=return_Gn(num,8)*2**7+return_Gn(num,7)*2**6+return_Gn(num,6)*2**5+return_Gn(num,5)*2**4+return_Gn(num,4)*2**3+return_Gn(num,3)*2**2+return_Gn(num,2)*2+return_Gn(num,1)
    print(result)
def get_hiaming_id(num):
    len=num.__len__()+1
    if len==1:
        return 2
    elif 2<=len<=4:
        return 3
    elif 5<=len<=11:
        return 4
    elif 12<=len<=26:
        return 5
    elif 27<=len<=57:
        return 6
    elif 58<=len<=120:
        return 7
    elif 121<=len<=247:
        return 8
def return_Gn(list,n):
    result=0
    list_result=[]
    start=int(2**(n-1))
    end=int(2*2**(n-1))
    tiao = int(2 ** (n - 1))
    if start<=list.__len__():
        if end > list.__len__():
            end = list.__len__() + 1
        while(start<=list.__len__()):
                for i in range(start,end):
                    list_result.append(list[i-1])
                start=end+tiao
                end=start+tiao
                if end>list.__len__():
                    end=list.__len__()+1
        for item in list_result:
            if item =='1':
                result+=1
        return result%2
    else:
        return 0

海明码插入

def get_hiaming_id(num):
    len=num.__len__()+1
    if len==1:
        return 2
    elif 2<=len<=4:
        return 3
    elif 5<=len<=11:
        return 4
    elif 12<=len<=26:
        return 5
    elif 27<=len<=57:
        return 6
    elif 58<=len<=120:
        return 7
    elif 121<=len<=247:
        return 8
def build_haiming():
    str=input("请输入您要创建海明码的01串")
    str_1=[]
    id_1=get_hiaming_id(str)
    id_charu=[]
    for i in range(id_1):
        id_charu.append(2**i)
    # print('id_charu',id_charu)
    j=0
    for i in range(str.__len__()+id_charu.__len__()):
        if i+1 not in id_charu:
            str_1.append(str[j])
            j+=1
        else:
            str_1.append(' ')
    # print(str_1)
    for i in range(1,id_1+1):
        if 2**(i-1)<=str_1.__len__():
            # print(i)
            str_1[2**(i-1)-1]=build_p(str_1,i)
    result_return=''
    for item in str_1:
        result_return+=item
    print(result_return)
def build_p(list,n):
    list_result = []
    start = int(2 ** (n - 1))
    end = int(2 * 2 ** (n - 1))
    tiao = int(2 ** (n - 1))
    if start <= list.__len__():
        if end > list.__len__():
            end = list.__len__() + 1
        else:
            end=end
        while (start <= list.__len__()):
            #print('start:',start-1,'end',end-1)
            list_result.extend(list[start-1:end-1])
            start = end + tiao
            end = start + tiao
            if end > list.__len__():
                end = list.__len__() + 1
        result=0
        for item in list_result:
            if item ==" ":
                result+=0
            else:
                result+=int(item)
        # print(result)
        return str(result%2)

crc8代码实现

def getyu(str1, str2):
    index = 0
    str1_new = ''
    for item in str1:
        if item == '0':
            index += 1
        else:
            break
    if index >= str2.__len__() - 1:
        return True, str1[-8:]
    else:
        for i in range(index, index + 9):
            if str1[i] == str2[i - index]:
                str1_new += '0'
            else:
                str1_new += '1'
        str1 = str1[:index] + str1_new + str1[index + 9:]
        return False, str1


def crc8():
    str_all = input("请输入您要传输的信息")
    str_ = ''
    str_list = []
    for i in range(8):
        str_ += '0'
    for item in str_all:
        item = str(bin(ord(item))[2:]) + str_
        str_list.append(item)
    in_ = 0
    for item in str_list:
        in_ += 1
        str1 = (False, item)
        str2 = '100000111'
        while (True):
            str1 = getyu(str1[1], str2)
            if str1[0] == True:
                break
        str_send = item[:-8] + str1[1]
        print('第{0}个元素的冗余码是{1},发送码是{2}'.format(in_, str1[1], str_send))

主代码展示

# 编写时间2021/6/16;14:07
# 编写  :刘钰琢
def bit():
def sure_haiming():
def return_Gn(list,n):
def get_hiaming_id(num):
def build_haiming():
def build_p(list,n):
def main():
    bit()#零比特填充
    sure_haiming()#验证海明码哪一步是错误的
    build_haiming()#创建海明码
    crc8()#crc8 实现
    pass
if __name__=='__main__':
    main()

总结


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}