Generating Random text file of size x bits
조회 수: 4 (최근 30일)
이전 댓글 표시
I would like to create a txt of strings/numbers that is of size x bits given the value of x
say if x = 245760 bits
This is to embed an image with x bits message.
댓글 수: 0
답변 (1개)
Ayush
2024년 10월 21일
Hi,
To generate a random text file of a specified size in bits, you first need to convert the size from bits to bytes, as file sizes are typically measured in bytes. Since 1 byte equals 8 bits, a file size of 245,760 bits translates to 30,720 bytes (245,760 bits / 8 bits per byte). You can generate random alphanumeric characters, with each character typically occupying 1 byte. By writing these characters to a text file, you can achieve the desired file size.
Refer to the example code below:
% Desired file size in bits
x_bits = 245760;
% Convert bits to bytes
x_bytes = x_bits / 8;
% Generate random alphanumeric characters
% Use ASCII range for alphanumeric characters: 48-57 (0-9), 65-90 (A-Z), 97-122 (a-z)
characters = ['0':'9' 'A':'Z' 'a':'z'];
num_chars = length(characters);
% Generate a random string of the required length
random_string = characters(randi(num_chars, 1, x_bytes));
% Write to a text file
fileID = fopen('random_text.txt', 'w');
fwrite(fileID, random_string);
fclose(fileID);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!