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

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:
read file,
user selects bit mode (4 options)
if 16bit --> rand
if 32bit --> rand
if 64bit -->rand
if 128bit -->rand
if anything else, error

7 commentaires

It might help if you attach an example file.
My apologies, here is a shortened versionof the textfile
How are the values delimited? What structure does the file have, or is it just one line of values?
Please upload a sample file by clicking on the paperclip button.
The file is attached here in the comments and I have updated the original. But basically it is 4 values per a line. This text file is a sample and the real one has a lot more, but it will still be 4 hex values per a line, then enter.
So, can we assume the "real" text file simply repeats the organization in the example file? That is, lines 1, 5, 9, etc., contain 16-bit hex values, lines 2, 6, 10, etc., contain 32-bit hex values, and so on? And can we assume the user input is one of 16, 32, 64, or 128?
@Scott MacKenzie Actually, it's more like
lines 1-20 are all 16bit
lines 21-40 are all 32bit
lines 41-60 are all 64bit
lines 61-80 are all 128bit
Sorry for the lack of context in my question! I hope this helps convey. Thanks so much for helping me
OK thanks. I see that @David Hill has just posted an succinct answer. Good luck.

Connectez-vous pour commenter.

 Réponse acceptée

r=randi(4);
a=readcell('hextest.txt');
x=input('hex length: ');
hexValue=a{log2(x/16)+1,r};

3 commentaires

Does hexValue=a{log2(x/16)+1,r} tell you which row to randomize from and it goes 1-4? That's a pretty good idea, I could format the rest of the values I have to just one line and keep the entire text file 4 lines.
If I wanted to print 4 random values from the line selected how could I do that?
Yes. If you don't want any repetitions, use randperm.
r=randperm(4,3);%selects 3 in random order from 1:4
a=readcell('hextest.txt');
x=input('hex length: ');
hexValue=a(log2(x/16)+1,r);%output is a cell array
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);
a=readcell('anothertest.txt');
x=input('Enter bit length: ');
hexValue=a{log2(x/16)+1,r};
fprintf('%s %s %s\n', hexValue);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by