Can somebody tell me how to print a string as bytes i.e. its corresponding ASCII code?!
My input is a normal string like "9" and the output should be the corresponding ASCII value of character '9'
If you're looking for a Byte Array - see this question: How to convert a Java String to an ASCII byte array?
To get the ascii value of each individual character you can do:
String s = "Some string here";
for (int i=0; i<s.length();i++)
System.out.println("ASCII value of: "+s.charAt(i) + " is:"+ (int)s.charAt(i) );
Use String.getBytes()
method.
byte []bytes="Hello".getBytes();
for(byte b:bytes)
System.out.println(b);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With