フィルターのクリア

ASCII TO HAMMING CODE 7.4

4 ビュー (過去 30 日間)
Branislav Gabco
Branislav Gabco 2023 年 2 月 21 日
編集済み: Voss 2023 年 2 月 25 日
Hi I want to make converter from ASCII to hamming code 7.4. This is my code but output from this code is incorrect because when I compare it with the results from the online converter, the results do not match. Can you please tell me what is wrong with this code?
% Define the generator matrix for Hamming code 7.4
G = [1 0 0 0 1 1 0;
0 1 0 0 1 0 1;
0 0 1 0 1 1 1;
0 0 0 1 0 1 1];
% Ask the user to input a text message
msg = input("Enter a text message: ", 's');
% Convert the message to ASCII codes
asciiMsg = uint8(msg);
% Convert the ASCII codes to binary and concatenate them into a single vector
binaryMsg = dec2bin(asciiMsg, 8) - '0';
binaryMsg = binaryMsg.';
% Pad the binary message with zeros if its length is not a multiple of 4
numPaddingZeros = mod(-length(binaryMsg), 4);
binaryMsg = [zeros(1, numPaddingZeros), binaryMsg(:).'].';
% Reshape the binary message into 4-bit blocks
binaryMsg = reshape(binaryMsg, 4, []).';
% Encode each 4-bit block using Hamming code 7.4
encodedMsg = mod(binaryMsg * G, 2);
% Convert the encoded message to a string of characters
encodedMsgStr = num2str(encodedMsg(:).');
encodedMsgStr = strrep(encodedMsgStr, ' ', '');
encodedMsgStr = regexprep(encodedMsgStr, '[^\d]', '');
% Print the encoded message
disp("Encoded message: " + encodedMsgStr);

採用された回答

Voss
Voss 2023 年 2 月 22 日
移動済み: Voss 2023 年 2 月 24 日
I would check that you're putting the elements of encodedMsg in the right order here:
encodedMsgStr = num2str(encodedMsg(:).');
encodedMsgStr = strrep(encodedMsgStr, ' ', '');
encodedMsgStr = regexprep(encodedMsgStr, '[^\d]', '');
Maybe it should be something like:
encodedMsgStr = reshape(char(encodedMsg.'+'0'),1,[]);
  2 件のコメント
Branislav Gabco
Branislav Gabco 2023 年 2 月 23 日
移動済み: Voss 2023 年 2 月 24 日
Thank you for your advice.
Your advice really helped me. It finally works as it should. Thank you very much.
Voss
Voss 2023 年 2 月 24 日
編集済み: Voss 2023 年 2 月 25 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHamming についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by