473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question re byte arrays and strings

Hi all,

This is probably a real dumb question, but I just haven't come across the
answer...

Is there a simple way to treat a byte array as a string, or to convert it to
a string? And the converse would sometimes be useful too, i.e.
convert/treat string as byte array.

Thanks
Mar 16 '06 #1
1 1426
"Steve Marshall" <st*****@westnet.net.au> schrieb
Hi all,

This is probably a real dumb question, but I just haven't come
across the answer...

Is there a simple way to treat a byte array as a string, or to
convert it to a string? And the converse would sometimes be useful
too, i.e.
convert/treat string as byte array.


Good question, not dumb question. :-)

Have a look at System.Text.Encoding.GetBytes and
System.Text.Encoding.GetString.

There are some predefined Encoding objects like
System.Text.Encoding.Unicode or System.Text.Encoding.Default

You must be aware of the fact that there are many code pages. For example, a
DOS code page has 1 byte per character whereas Unicode is always 2 bytes per
character. Strings are always stored as Unicode in .Net. There are even
different DOS code pages, like US or Westeuropean. So, the number of a
character (= the character code) can be different in different code pages.
For example, the Euro sign "€" has character code &H80 in code page 1252
("Westeuropean ´(Windows)") whereas it has character code &H20AC in Unicode.

When converting from a byte array to a string, you must always know which
code page has been used to encode the array. Same in the opposite direction:
You must choose the appropriate destination encoding.

Example:

Dim b(0) As Byte
Dim s As String

b(0) = 128

s = System.Text.Encoding.Default.GetString(b)
MsgBox(s)

Armin

Mar 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
2
by: andy.dreistadt | last post by:
Hi all, I came across another problem that is probably pretty easy but, again, due to my rusty-ness with C, I'm a little stumped. I have a struct that looks like this: /* Instrument Data...
30
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
4
by: movieknight | last post by:
Hi, I have an application that stores raw .wav files (and also jpgs/bitmaps) within strings, and I need to sometimes convert these strings to byte arrays, and sometimes go from byte arrays back...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.