建立帶校驗位的字串

2021-08-15 17:18:34 字數 995 閱讀 1669

在平時傳輸資料時經常會遇見資料接受內容有誤的情況,所以簡單起見可以自己新增校驗位進行傳輸,此次只是對字串型別進行舉例,完成校驗位的新增,方法比較簡單,如果需要更加校驗精確可以在校驗位數和生成校驗碼的方式上進行完善,鄙人比較喜歡用crc方式。

using system;

using system.collections.generic;

using system.text;

namespace checkdata

/// /// 解碼建帶data校驗的字串

///

/// 需要解碼data校驗的字串內容

/// 需要解碼字串內容原長度(length),若無固定長度寫為0

/// 返回值為新增末位校驗碼的字串(null為傳入字串內容為空,error為解碼字串長度或內容有誤)

public static string data_deal(string obj, int num)

else if (num == 0 || obj.length == (num + 1))

catch (formatexception ex)

if (deal_data_code(obj.substring(0, obj.length - 1)) == num_buff)

return obj.substring(0, obj.length - 1);

else

return "error";

}return "error";

}//將字串進行ascii轉碼,通過反覆自加得到個位數作為校驗碼追加到字串尾部

private static int deal_data_code(string buff)

if (obj_buff > 9)

} while (obj_buff > 9);

}return obj_buff;}}

}

已封裝資源

已封裝資源

shell 批量建立帶隨機字串的html檔案

思路一 echo random md5sum tr 0 9 a j cut c 2 11 random linux內建的隨機數變數 預設是5個 md5sum 使用md5加密,變成隨機的字母 tr 替換。把0 9 替換成a j cut 截取出2 11位 1 bin sh23 4 d test mkdi...

串列埠通訊中資料的奇偶校驗位演算法

奇偶校驗位是乙個表示給定位數的二進位制數中 1 的個數是奇數還是偶數的二進位制數。奇偶校驗位是最簡單的錯誤檢測碼。奇偶校驗位有兩種型別 偶校驗位與奇校驗位。如果一組給定資料位中 1 的個數是奇數,那麼偶校驗位就置為 1,從而使得總的 1 的個數是偶數。如果給定一組資料位中 1 的個數是偶數,那麼奇校...

字串的建立

string字串的建立可以通過string str1 hello 或者 string str2 new string hello 兩種形式。使用string str1 hello 這種方式建立字串的時候,jvm首先會檢查字串常量池中是否存在該字串的物件,如果已經存在,那麼就不會在字串常量池中再建立了...