0

Is there any "cleaner" way to do the following?

byte b;
int chLen = /*for example*/ (int)(Math.random() * 10);
b = ("" + (int)(char)chLen).getBytes()[0];

This, essentially, takes an int that is >= 0 and < 10 and gets its ASCII code and stores it into a byte, but it looks like there is a better method. Any ideas???

1
  • To test this you can use System.out.println(chLen); System.out.println(b); System.out.println((char)b); and the first and last output should be the same while the middle input should be different. Commented Jul 12, 2014 at 3:44

3 Answers 3

2

Perhaps you're looking for something like n + '0', where n is an int between 0 and 9?

Sign up to request clarification or add additional context in comments.

5 Comments

Or for that matter, n | '0'. Since the ASCII codes for '0' through '9' are 0x30 through 0x39, you don't need to carry. (Not that this will save you any cycles in most modern machines.) ((Typo corrected.))
Yup! Thank you. Quick question, though. What? '0' is null (in terms of char), right? So, how does adding null char change the int...?
@dylnmc While the byte value 0 is typically used as a null string terminator in many programming languages, don't confuse that with the zero character '0' (note the single quotes mark this as a character), which when encoded in ASCII is the decimal value 48 (or hex value 0x30).
@dylnmc '\0' is not '0'
Oh... gotcha! Silly me; so '0' basically (when typecast to an int or byte) gets converted to the ASCII code -- the decimal representation of a char -- and '0' (whose decimal ASCII code is 48) is the first on the list, so an int n (let's say n = 1) is added to 48 to get the ASCII code 49 which does, in fact, represent '1'. Right... or left?
0

How about below:

byte[] bytes = ByteBuffer.allocate(4).putInt(chLen).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

Comments

0

It sounds like you want a random number from 0 to 9 and then add it to '0', I'd use Random.nextInt(10) to get such a number,

Random rand = new Random();
int ch = '0' + rand.nextInt(10);
System.out.printf("'%s' = %d (dec), %s (hex)%n",
    String.valueOf((char) ch), ch,
    Integer.toHexString(ch));

Since it's difficult to debug random code, I also wrote a simple unit test,

for (int ch = '0'; ch <= '9'; ch++) {
  System.out.printf("'%s' = %d (dec), %s (hex)%n",
      String.valueOf((char) ch), ch,
      Integer.toHexString(ch));
}

Output is

'0' = 48 (dec), 30 (hex)
'1' = 49 (dec), 31 (hex)
'2' = 50 (dec), 32 (hex)
'3' = 51 (dec), 33 (hex)
'4' = 52 (dec), 34 (hex)
'5' = 53 (dec), 35 (hex)
'6' = 54 (dec), 36 (hex)
'7' = 55 (dec), 37 (hex)
'8' = 56 (dec), 38 (hex)
'9' = 57 (dec), 39 (hex)

2 Comments

:P I didn't know you could use printf() with with Java. I thought that was a C-thing... even though Java is based off C, I've still never seen printf(String s, Object args) in Java like in C. Also, isn't (int)(Math.random() * 10) the same exact thing as Random rand = new Random(); rand.nextInt(10); I've always used min + (int)(Math.random() + (max - min + 1)); but I guess it's just a matter of preference.
@dylnmc Java added formatted printf (and String.format) in version (1.)5; As for using the Object instead of the static method, well - I suppose that is a matter of preference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.