Re: Unwrapping Java Character to char

Blake McBride <[email protected]> Wed, 3 Jan 2024 15:46:27 -0600
Newsgroups gmane.editors.j.devel
Message-ID <CABwHSOtZS6GbEpM8ytjnBfpRSXgFvaeXcPiaJVkUbbWaRH2kpQ__3400.7317897462$1704318447$gmane$org@mail.gmail.com>
--000000000000be9f56060e118c6c
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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 <[email protected]=
om>
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
>

--000000000000be9f56060e118c6c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hey Robert,<div><br></div><div>In Java, both Character and=
 char are Unicode.=C2=A0 So, you can unbox Character to char but you&#39;ll=
 still have unicode.</div><div><br></div><div>Also, although it intuitively=
 seems like converting unicode to ascii should be easy, I&#39;ve spens a bu=
nch of time 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.Patter=
n;<br><br>public class UnicodeToAsciiConverter {<br><br>=C2=A0 =C2=A0 publi=
c static String convertToAscii(String unicodeStr) {<br>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 String normalized =3D Normalizer.normalize(unicodeStr, Normalizer.F=
orm.NFD);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Pattern pattern =3D Pattern.compil=
e(&quot;[^\\p{ASCII}]&quot;);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return pattern=
.matcher(normalized).replaceAll(&quot;&quot;);<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 &quot;H=C3=A9llo, W=C3=B6rld! =F0=9F=91=
=8B&quot;; // Example Unicode string<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 String =
asciiString =3D convertToAscii(unicodeString);<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 System.out.println(&quot;Original Unicode String: &quot; + unicodeStrin=
g);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println(&quot;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=AFP=
M Robert Dodier &lt;<a href=3D"mailto:[email protected]">robert.dodie=
[email protected]</a>&gt; 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">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>

--000000000000be9f56060e118c6c--