Re: Unwrapping Java Character to char
Blake McBride <[email protected]> Wed, 3 Jan 2024 15:57:24 -0600
| Newsgroups | gmane.editors.j.devel |
|---|---|
| Message-ID | <CABwHSOsAgddDz3kw8kRdaTYE4WLZhJR4xgskNKa-QtMKAP1oRA__19413.2873484463$1704319110$gmane$org@mail.gmail.com> |
--000000000000e06d03060e11b322 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Java, auto-unboxes. So, you can do the following: Character co =3D 'A'; char c =3D co; On Wed, Jan 3, 2024 at 3:51=E2=80=AFPM Blake McBride <[email protected]> w= rote: > Sorry for the confusing message, although it is correct. When I said I > spent a bunch of time on it with an incomplete solutions, I was talking > about JavaScript. > > So, except for the paragraph that starts with "Also, ...", the rest is > correct. > > --blake > > > On Wed, Jan 3, 2024 at 3:46=E2=80=AFPM Blake McBride <[email protected]>= wrote: > >> Hey Robert, >> >> In Java, both Character and char are Unicode. So, you can unbox >> Character to char but you'll still have unicode. >> >> Also, although it intuitively seems like converting unicode to ascii >> should be easy, I've spens a bunch of time with this and was never able = to >> do it completely. Here is what I have been using: >> >> From chatGPT: >> >> import java.text.Normalizer; >> import java.util.regex.Pattern; >> >> public class UnicodeToAsciiConverter { >> >> public static String convertToAscii(String unicodeStr) { >> String normalized =3D Normalizer.normalize(unicodeStr, >> Normalizer.Form.NFD); >> Pattern pattern =3D Pattern.compile("[^\\p{ASCII}]"); >> return pattern.matcher(normalized).replaceAll(""); >> } >> >> public static void main(String[] args) { >> String unicodeString =3D "H=C3=A9llo, W=C3=B6rld! =F0=9F=91=8B";= // Example Unicode >> string >> String asciiString =3D convertToAscii(unicodeString); >> System.out.println("Original Unicode String: " + unicodeString); >> System.out.println("Converted ASCII String: " + asciiString); >> } >> } >> >> --blake >> >> >> On Wed, Jan 3, 2024 at 2:27=E2=80=AFPM Robert Dodier <robert.dodier@gmai= l.com> >> wrote: >> >>> Hi, is there a way to unwrap a Java boxed value to get the Java >>> primitive value inside it? >>> >>> In particular I am interested in obtaining the char inside a >>> Character, in order to supply the char to a method which requires a >>> char. >>> >>> Thanks for any info, >>> >>> Robert >>> >> --000000000000e06d03060e11b322 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Java, auto-unboxes.=C2=A0 So, you can do the following:<di= v><br></div><div>Character co =3D 'A';</div><div>char c =3D co;</di= v></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr= ">On Wed, Jan 3, 2024 at 3:51=E2=80=AFPM Blake McBride <<a href=3D"mailt= o:[email protected]">[email protected]</a>> wrote:<br></div><blockquot= e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s= olid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Sorry for the conf= using message, although it is correct.=C2=A0 When I said I spent a bunch of= time on it with an incomplete solutions, I was talking about JavaScript.<d= iv><br></div><div>So, except for the paragraph that starts with "Also,= ...", the rest is correct.</div><div><br></div><div>--blake</div><div= ><br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"g= mail_attr">On Wed, Jan 3, 2024 at 3:46=E2=80=AFPM Blake McBride <<a href= =3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>>= wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px = 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir= =3D"ltr">Hey Robert,<div><br></div><div>In Java, both Character and char ar= e Unicode.=C2=A0 So, you can unbox Character to char but you'll still h= ave unicode.</div><div><br></div><div>Also, although it intuitively seems l= ike converting unicode to ascii should be easy, I've spens a bunch of t= ime with this and was never able to do it completely.=C2=A0 Here is what I = have been using:</div><div><br></div><div>From chatGPT:</div><div><br></div= ><div>import java.text.Normalizer;<br>import java.util.regex.Pattern;<br><b= r>public class UnicodeToAsciiConverter {<br><br>=C2=A0 =C2=A0 public static= String convertToAscii(String unicodeStr) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 = String normalized =3D Normalizer.normalize(unicodeStr, Normalizer.Form.NFD)= ;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Pattern pattern =3D Pattern.compile("= [^\\p{ASCII}]");<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return pattern.matcher= (normalized).replaceAll("");<br>=C2=A0 =C2=A0 }<br><br>=C2=A0 =C2= =A0 public static void main(String[] args) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0= String unicodeString =3D "H=C3=A9llo, W=C3=B6rld! =F0=9F=91=8B";= // Example Unicode string<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 String asciiStrin= g =3D convertToAscii(unicodeString);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 System.= out.println("Original Unicode String: " + unicodeString);<br>=C2= =A0 =C2=A0 =C2=A0 =C2=A0 System.out.println("Converted ASCII String: &= quot; + asciiString);<br>=C2=A0 =C2=A0 }<br>}<br></div><div><br></div><div>= --blake</div><div><br></div></div><br><div class=3D"gmail_quote"><div dir= =3D"ltr" class=3D"gmail_attr">On Wed, Jan 3, 2024 at 2:27=E2=80=AFPM Robert= Dodier <<a href=3D"mailto:[email protected]" target=3D"_blank">ro= [email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_qu= ote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,20= 4);padding-left:1ex">Hi, is there a way to unwrap a Java boxed value to get= the Java<br> primitive value inside it?<br> <br> In particular I am interested in obtaining the char inside a<br> Character, in order to supply the char to a method which requires a<br> char.<br> <br> Thanks for any info,<br> <br> Robert<br> </blockquote></div> </blockquote></div> </blockquote></div> --000000000000e06d03060e11b322--