Conversion of Array[Byte] to ASCII values

3,963 views
Skip to first unread message

jitendra shelar

unread,
Jun 30, 2014, 2:19:47 AM6/30/14
to scala...@googlegroups.com
Hi,

I am newbie to scala.
I want to convert below array of byte into ASCII representation.

ct = [A@ce7e26a   // ct : Array[Byte]

Something like  var ascii = Array(23,65,45,33,..).

And back from ASCII array to original array of byte.

Can somebody please help me on this?

Thanks,
Jitendra

Michal Rus

unread,
Jun 30, 2014, 2:28:46 AM6/30/14
to jitendra shelar, scala-user
On Mon, Jun 30, 2014 at 8:19 AM, jitendra shelar
<jitendra....@gmail.com> wrote:

> I want to convert below array of byte into ASCII representation.

scala> val bs = Array('a'.toByte, 'b'.toByte)
bs: Array[Byte] = Array(97, 98)

scala> val s = new String(bs, "UTF-8")
s: String = ab

m.

jitendra shelar

unread,
Jun 30, 2014, 2:35:50 AM6/30/14
to scala...@googlegroups.com
Thanks Michal. Thanks alot.
It solved my problem.

Regards,
Jitendra

jitendra shelar

unread,
Jun 30, 2014, 3:08:47 AM6/30/14
to scala...@googlegroups.com
Hi Michal,

I had few queries.

a) Are  ASCII values and Byte values same?
 
b) And why "utf-8" encoding was used in above case? and what is use of this encoding?
scala> val s = new String(bs, "UTF-8") 
s: String = ab 

Thanks & Regards,
Jitendra

On Monday, June 30, 2014 11:49:47 AM UTC+5:30, jitendra shelar wrote:

Michal Rus

unread,
Jun 30, 2014, 10:14:00 AM6/30/14
to jitendra shelar, scala-user
On Mon, Jun 30, 2014 at 9:08 AM, jitendra shelar
<jitendra....@gmail.com> wrote:

> Hi Michal,

Hey, Jitendra,

> a) Are ASCII values and Byte values same?

You mean... whether Char and Byte types represent the same concept? If
so, no, a Byte is just a raw byte, one of 256 possible ones. A Char,
on the other hand may hold a UTF-8-encoded character, like 'Ç'. You
need two bytes to represent this one.

> b) And why "utf-8" encoding was used in above case? and what is use of this
> encoding?

I think this one should be clear now.

Best,
m.

Som Snytt

unread,
Jun 30, 2014, 10:17:02 AM6/30/14
to Michal Rus, jitendra shelar, scala-user
"US-ASCII" is also a required charset.




--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Ruebenacker

unread,
Jun 30, 2014, 10:35:15 AM6/30/14
to jitendra shelar, scala-user

     Hello,

On Mon, Jun 30, 2014 at 3:08 AM, jitendra shelar <jitendra....@gmail.com> wrote:
Hi Michal,

I had few queries.

a) Are  ASCII values and Byte values same?

  Historically, ASCII was defined as a set of 128 characters, including control characters, each corresponding to one value of the byte, which was chosen as seven bits (values from 0 to 127).

  Then, byte became established to be eight bits, with 256 values from 0 to 255 (or -128 to 127 if used as a signed type, like in Java/Scala). This led to extended ASCII character sets, where each character corresponded to one byte value. There were multiple of these, mostly to accommodate different (mostly European) languages that use alphabets similar to the English one (e.g. Spanish, French, German, Turkish). Byte values from 0 to 127 would be universal, and for byte values from 128 to 255 (or -128 to -1), you would have to specify the character set.

  Then came Unicode to accommodate all frequently used characters in the world: Chinese, Japanese, Cyrillic, Korean, Arabic, etc.

  Now, there are different ways to map bytes to unicode characters, and UTF-8 is one of them.

  And then came Java as the first widely used language allowing source code containing unicode characters.

 
b) And why "utf-8" encoding was used in above case? and what is use of this encoding?
scala> val s = new String(bs, "UTF-8") 
s: String = ab 

  UTF-8 is popular, because it maps byte values from 0 to 127 to ASCII characters and uses sequences of byte values from 128 to 255 for all the other characters. This means text files that are valid ASCII (not extended ASCII, though) are now valid UTF-8 (though not the other way round).

     Best,
     Oliver

 

Thanks & Regards,
Jitendra

On Monday, June 30, 2014 11:49:47 AM UTC+5:30, jitendra shelar wrote:
Hi,

I am newbie to scala.
I want to convert below array of byte into ASCII representation.

ct = [A@ce7e26a   // ct : Array[Byte]

Something like  var ascii = Array(23,65,45,33,..).

And back from ASCII array to original array of byte.

Can somebody please help me on this?

Thanks,
Jitendra

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Be always grateful, but never satisfied.

jitendra shelar

unread,
Jul 1, 2014, 10:51:44 AM7/1/14
to scala...@googlegroups.com
Now concepts are clear to me.
Thanks all for your help.

Regards,
Jitendra

On Monday, June 30, 2014 11:49:47 AM UTC+5:30, jitendra shelar wrote:
Reply all
Reply to author
Forward
0 new messages