In Java String.toCharArray(), " " become '' - char

I am trying to go through a string one character at a time and change those characters into their ASCII numbers. I have it working wine except for the spaces in the text turned into '' (with no space) instead of ' ' (with space).
Scanner in = new Scanner(original_file);
PrintWriter out = new PrintWriter("encoded_message.txt");
text_file = "";
while (in.hasNextLine()) {
text_file += in.nextLine() + "\n";
}
char[] text_file_array = text_file.toCharArray();
for (char c : text_file_array) {
int code = Character.getNumericValue(c); //<-- The problem is here.
out.println(code);
}
in.close();
out.close();
When I run this, the c becomes '' which makes code have a value of -1. I later use code in an array. How do I make it return ' '?

The Character.getNumericValue() method returns the unicode numeric value not the ASCII value.
e.g. for 'e' ASCII is 101, unicode is 14.
ASCII is relatively simple:
int ascii = (int) character;
Convert character to ASCII numeric value in java
The javadoc for getNumericValue discusses this but sheds little light to the matter:
https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue%28char%29
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.
If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.
The c doesn't in fact change to become '' (with no space). If you change your out.println method to this (debugging, yes? you can clean it up later), you can get more info:
out.println("'" + c + "'" + " " + code + " " + (int)c);

Related

VBScript: How can I trim a number to 4 decimal places but not round it?

My code currently looks like this:
FormatNumber((CDbl(0.05935)),4)
The returned value is 0.0594 rather than 0.0593 which is what I need.
You can try parsing this number to string then trimming it and again parsing back to float.
example:
v = 100.0097
x = Str$(v) ' Gives " 100.0097"
//This adds a leading space for positive numbers
or
x = CStr(v) ' Gives "100.0097"
and then trim it as your need
finalstr = LEFT(variable, (LEN(variable)-4))
then parse it to float
finaltrimed = CDbl(finalstr)

How to split string by HEX character

I need help to split string which contains a HEX character.
How to do it?
The code I have got doesnt work.
dim itr
itr="343434 XX7777777" ' SI
msgbox itr
dim scrns
scrns=Split(itr,"SI",-1,1)
msgbox scrns(1)
Define the character by its numeric value (0x0f for "shift in"):
scrns = Split(itr, Chr(&h0f))
In VBScript you define hexadecimal numbers by prefixing them with &h. The Chr function turns the number into the corresponding character.
As #JNevill pointed out in the comments you could also use the decimal instead of the hexadecimal value:
scrns = Split(itr, Chr(15))

How to print/scan Underscores in Numeric Literals

Team,
I am not able to use the Java 7 Underscores in Numeric Literals feature for getting the input from user and printing out in same format as declared. Please help in doing that? OR Is this feature is incomplete?
Scanner input = new Scanner( System.in );
int x = 1_00_000;
System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
x = input.nextInt(); //java.util.InputMismatchException
System.out.println(x); // Prints in normal format, but want to be in 2_00_000.
NOTE: In Eclipse; I am able to change the value of numeric literal with Underscored numeric literal in runtime. This may be hack, but this is needed feature to input Underscored numeric literal in runtime rit?.
http://www.eclipse.org/jdt/ui/r3_8/Java7news/whats-new-java-7.html#miscellaneous
if you want maintain the underscores you can use String:
Scanner input = new Scanner( System.in );
System.out.print( "Enter numeric literals with underscores: " ); //2_00_000
String stringLiterals = input.nextLine();
System.out.println(stringLiterals); // Prints 2_00_000.

Converting Characters to ASCII Code & Vice Versa In C++/CLI

I am currently learning c++/cli and I want to convert a character to its ASCII code decimal and vice versa( example 'A' = 65 ).
In JAVA, this can be achieved by a simple type casting:
char ascci = 'A';
char retrieveASCII =' ';
int decimalValue;
decimalValue = (int)ascci;
retrieveASCII = (char)decimalValue;
Apparently this method does not work in c++/cli, here is my code:
String^ words = "ABCDEFG";
String^ getChars;
String^ retrieveASCII;
int decimalValue;
getChars = words->Substring(0, 1);
decimalValue = Int32:: Parse(getChars);
retrieveASCII = decimalValue.ToString();
I am getting this error:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Any Idea on how to solve this problem?
Characters in a TextBox::Text property are in a System::String type. Therefore, they are Unicode characters. By design, the Unicode character set includes all of the ASCII characters. So, if the string only has those characters, you can convert to an ASCII encoding without losing any of them. Otherwise, you'd have to have a strategy of omitting or substituting characters or throwing an exception.
The ASCII character set has one encoding in current use. It represents all of its characters in one byte each.
// using ::System::Text;
const auto asciiBytes = Encoding::ASCII->GetBytes(words->Substring(0,1));
const auto decimalValue = asciiBytes[0]; // the length is 1 as explained above
const auto retrieveASCII = Encoding::ASCII->GetString(asciiBytes);
Decimal is, of course, a representation of a number. I don't see where you are using decimal except in your explanation. If you did want to use it in code, it could be like this:
const auto explanation = "The encoding (in decimal) "
+ "for the first character in ASCII is "
+ decimalValue;
Note the use of auto. I have omitted the types of the variables because the compiler can figure them out. It allows the code to be more focused on concepts rather than boilerplate. Also, I used const because I don't believe the value of "variables" should be varied. Neither of these is required.
BTW- All of this applies to Java, too. If your Java code works, it is just out of coincidence. If it had been written properly, it would have been easy to translate to .NET. Java's String and Charset classes have very similar functionality as .NET String and Encoding classes. (Encoding to the proper term, though.) They both use the Unicode character set and UTF-16 encoding for strings.
More like Java than you think
String^ words = "ABCDEFG";
Char first = words [0];
String^ retrieveASCII;
int decimalValue = ( int)first;
retrieveASCII = decimalValue.ToString();

Extended Ascii codes incomplete DART? No character from 128 to 160

I created a small piece of code to print the extended ASCII characters in DART but it seems the ones between 128 and 160 are blank.
PrintExtendedASCII(){
var listCodes = new List();
for (var i = 128; i < 256 ; i++) {
listeCodes.add(i);
}
var list = new String.fromCharCodes(listCodes);
print(list);
}
It only prints :  ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
Is there something different about the extended ASCII characters in DART?
There is no "extended ASCII" in Dart. The character codes you are using in the code example are not ASCII - they are Unicode code points. For code points 0-127, the character codes match ASCII exactly. The block you are missing, from 128 to 160 (0x80 to 0x9F), is all non-printable control characters.
Here is a table of Unicode code points for the 0x000-0xFFF block. If you look carefully, the order of characters exactly matches the string printed on your machine.

Resources