Re: Unwrapping Java Character to char

Blake McBride <[email protected]> Wed, 3 Jan 2024 15:51:42 -0600
Newsgroups gmane.editors.j.devel
Message-ID <CABwHSOvqmFbQ87epa_3doNt_M7rvDcpB-FmN1_WmAgg6VBUe5w__17176.6240893461$1704318760$gmane$org@mail.gmail.com>
--0000000000007eb341060e119fab
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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]> w=
rote:

> Hey Robert,
>
> In Java, both Character and char are Unicode.  So, you can unbox Characte=
r
> 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 t=
o
> 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@gmail=
.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
>>
>

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

<div dir=3D"ltr">Sorry for the confusing message, although it is correct.=
=C2=A0 When I said I spent a bunch of time on it with an incomplete solutio=
ns, I was talking about JavaScript.<div><br></div><div>So, except for the p=
aragraph that starts with &quot;Also, ...&quot;, the rest is correct.</div>=
<div><br></div><div>--blake</div><div><br></div></div><br><div class=3D"gma=
il_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Jan 3, 2024 at 3:46=
=E2=80=AFPM Blake McBride &lt;<a href=3D"mailto:[email protected]">blake@m=
cbride.name</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"><div dir=3D"ltr">Hey Robert,<div><br></div><div>In Java, both Ch=
aracter 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 bunch 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.r=
egex.Pattern;<br><br>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 P=
attern.compile(&quot;[^\\p{ASCII}]&quot;);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 r=
eturn 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=B6=
rld! =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; =
+ unicodeString);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println(&quot;C=
onverted 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 &lt;<a href=3D"mailto:[email protected]=
" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div><blockq=
uote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1p=
x solid rgb(204,204,204);padding-left:1ex">Hi, is there a way to unwrap a J=
ava 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>

--0000000000007eb341060e119fab--