Re: Unwrapping Java Character to char
Blake McBride <[email protected]> Wed, 3 Jan 2024 15:59:30 -0600
| Newsgroups | gmane.editors.j.devel |
|---|---|
| Message-ID | <CABwHSOvQBYEHSXTeEGc7XVHD7yOCGr-0TD4fVp+pssvYFez71w__27839.9936201581$1704319227$gmane$org@mail.gmail.com> |
--0000000000006605fd060e11bb4b
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
However, remember that "co" can be null and "c" can't. So, you may have to
do something like:
if (co =3D=3D null)
c =3D myDefault;
else
c =3D co;
On Wed, Jan 3, 2024 at 3:57=E2=80=AFPM Blake McBride <[email protected]> w=
rote:
> 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]>=
wrote:
>
>> 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@gma=
il.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
>>>>
>>>
--0000000000006605fd060e11bb4b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">However, remember that "co" can be null and &quo=
t;c" can't.=C2=A0 So, you may have to do something like:<div><br><=
/div><div>if (co =3D=3D null)</div><div>=C2=A0 =C2=A0 c =3D myDefault;</div=
><div>else</div><div>=C2=A0 c =3D co;</div><div><br></div></div><br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Jan 3, 20=
24 at 3:57=E2=80=AFPM Blake McBride <<a href=3D"mailto:[email protected]=
e">[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"><div dir=3D"ltr">Java, auto-unboxes.=C2=A0 So, you can=
do the following:<div><br></div><div>Character co =3D 'A';</div><d=
iv>char c =3D co;</div></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"mailto:[email protected]" target=3D"_blank">blake@mcbride.=
name</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1=
ex"><div dir=3D"ltr">Sorry for the confusing message, although it is correc=
t.=C2=A0 When I said I spent a bunch of time on it with an incomplete solut=
ions, I was talking about JavaScript.<div><br></div><div>So, except for the=
paragraph that starts with "Also, ...", the rest is correct.</di=
v><div><br></div><div>--blake</div><div><br></div></div><br><div class=3D"g=
mail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, Jan 3, 2024 at 3:=
46=E2=80=AFPM Blake McBride <<a href=3D"mailto:[email protected]" targe=
t=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 rg=
b(204,204,204);padding-left:1ex"><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'll still have unicode.</div><div><br></div><=
div>Also, although it intuitively seems like converting unicode to ascii sh=
ould be easy, I've spens a bunch of time with this and was never able t=
o do it completely.=C2=A0 Here is what I have been using:</div><div><br></d=
iv><div>From chatGPT:</div><div><br></div><div>import java.text.Normalizer;=
<br>import java.util.regex.Pattern;<br><br>public class UnicodeToAsciiConve=
rter {<br><br>=C2=A0 =C2=A0 public static String convertToAscii(String unic=
odeStr) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 String normalized =3D Normalizer.n=
ormalize(unicodeStr, Normalizer.Form.NFD);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 P=
attern pattern =3D Pattern.compile("[^\\p{ASCII}]");<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 return pattern.matcher(normalized).replaceAll("&q=
uot;);<br>=C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 public static void main(Stri=
ng[] 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 asciiString =3D convertToAscii(unicodeSt=
ring);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 System.out.println("Original Uni=
code String: " + unicodeString);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 System=
.out.println("Converted ASCII String: " + asciiString);<br>=C2=A0=
=C2=A0 }<br>}<br></div><div><br></div><div>--blake</div><div><br></div></d=
iv><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:rob=
[email protected]" target=3D"_blank">[email protected]</a>> wro=
te:<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>
</blockquote></div>
</blockquote></div>
</blockquote></div>
--0000000000006605fd060e11bb4b--