How to write a program that selectively picks hexadecimal values from a text file based on bit size?

1 ビュー (過去 30 日間)
Akana Juliet 2021 年 6 月 15 日
コメント済み: Akana Juliet 2021 年 6 月 15 日
Hello all! I have a text file that is full of 16bit, 32bit, 64bit, and 128bit hexadecimal values, and I was trying to write a program that asks user the bit-size, then once the size is selected, a random hexadecimal value from the text file is spit out. I was thinking about if statements, but nothing I try is working. Any help or guidance would be greatly appreciated!
Edit Sorry! I have added the text file, but it is 4 hex values per a line, then enter. There are a lot more values than this in the real one but it follows same format
Edit#2
I am mostly confused with syntax for the bit selector, but I was thinking something like:
user selects bit mode (4 options)
if 16bit --> rand
if 32bit --> rand
if 64bit -->rand
if 128bit -->rand
if anything else, error
7 件のコメント表示非表示 6 件の古いコメント
Scott MacKenzie 2021 年 6 月 15 日
OK thanks. I see that @David Hill has just posted an succinct answer. Good luck.

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

採用された回答

David Hill 2021 年 6 月 15 日
r=randi(4);
x=input('hex length: ');
hexValue=a{log2(x/16)+1,r};
3 件のコメント表示非表示 2 件の古いコメント
Akana Juliet 2021 年 6 月 15 日
Thank you so much for your help @David Hill ! I just have one final question, what am I doing wrong with my print statement? I'm only seeing 1 random value posted?
r=randi(4,3);
x=input('Enter bit length: ');
hexValue=a{log2(x/16)+1,r};
fprintf('%s %s %s\n', hexValue);

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

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by