0

I always gets confused when people ask me to generate random string which is of 300 bytes or some predefined bytes. I am not sure what does they mean in general? Are they asking that string should be of 300 length?

I am working on a project in which people have asked me to generate random String of approximately 300 bytes.

Is it possible to do? I am confuse how we can generate random string of 300 bytes?

I know how to generate random string like this -

private static final Random random = new Random();
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

public static String generateString(int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
    }
    return sb.toString();
}

Can anyone explain me what does it mean when we need to generate random string of approx 300 bytes?

Jason C
  • 38,729
  • 14
  • 126
  • 182
john
  • 11,311
  • 40
  • 131
  • 251

3 Answers3

1

Strings are made of characters, but the number of bytes required to represent a character can be 1 or 2 (or sometimes more) depending on the character and the encoding. eg characters encoded in UTF8 that are over ascii 127 need 2 bytes, but those under - like english letters and numbers, take only 1.

Normally, string size refers to the number of characters. You only need the bytes if you are writing bytes, and the bytes you write depend on the encoding used.

I would interpret the requirement as 300 characters, especially since you have listed all candidate characters and they are 1-byte chars in the standard encoding.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks for suggestion. In our case we use `UTF-8` encoding. – john Nov 08 '14 at 06:10
  • @user2809564 I also agree that you should interpret the requirement as 300 characters, and I believe this answer is the correct answer. It is not uncommon for characters to be mistakenly referred to as "bytes", and also it would be an odd request (if you actually want 300 bytes, you'd want a `byte[300]` not a `String`). When in doubt, though, ask whoever told you what they really mean. – Jason C Nov 08 '14 at 06:24
-1

Since each hexadecimal digit represents four binary digits, which means two digits represent 1 byte. You can generate 300 random numbers Yi in hexadecimals. Such that, 0x41 <= Yi <= 0x5a. so that Yi maps to [A-Za-z] in UTF-8. (Change the range if you want to include numbers or any other characters).

and then you convert these numbers to String using Byte Encodings

ms2r
  • 170
  • 3
  • 17
-3

A java char is 2 bytes, the example is as follows:

public class Main {
public static void main(String[] args) {
    String str="Hello World" ;
    System.out.println(str.getBytes().length);
} 

}

The result is 11 .

Honest
  • 182
  • 11
  • Your own code and its output shows the chars you used are *one* byte, *not* 2: your answer is self contradictory (and necessarily wrong) – Bohemian Nov 08 '14 at 06:29
  • this just prints the length of the string NOT how much memory the string occupies: Minimum String memory usage (bytes) = 8 * (int) ((((no chars) * 2) + 45) / 8) – Hector Nov 08 '14 at 06:32
  • 1
    @Hector It does not "just print the length of the string" (nor does it print how much memory the string object occupies). It prints the number of bytes required to represent the string in the default character encoding, which may not be (and likely isn't) the same as the number of characters in the string. You seem to lack a fundamental understanding of how character encodings work, present in all of your comments. – Jason C Nov 08 '14 at 06:53