MidB in VB vs GetBytes in C#?

G

Guest

Hi there,

I have got the following piece of VB code to be translated to C#, I used
System.Text.Encoding.Default.GetBytes(myStr, 1, 10, aBytes, 0) but the result
of aBytes is different from what I have got in VB, please help! Thanks!

VB code:
Dim mystr As String
mystr = "<PURCHASE_ORDER partner=..."

Dim aByte() As Byte
ReDim aByte(10)

aByte = MidB(mystr, 1, 10)

The aByte(10) returns 60, 0, 80, 0, 85,0, 82, 0, 67,0

C# code:
Byte[] aByte = new Byte[10];
string myStr = "<PURCHASE_ORDER partner=...";
int iByteCount = System.Text.Encoding.Default.GetBytes(myStr, 1,10, aByte,0);

The aBtye returns 80,85,82,67,72,65,83,69,95,79
 
J

Jon Skeet [C# MVP]

cloudx said:
I have got the following piece of VB code to be translated to C#, I used
System.Text.Encoding.Default.GetBytes(myStr, 1, 10, aBytes, 0) but the result
of aBytes is different from what I have got in VB, please help! Thanks!

VB code:
Dim mystr As String
mystr = "<PURCHASE_ORDER partner=..."

Dim aByte() As Byte
ReDim aByte(10)

aByte = MidB(mystr, 1, 10)

The aByte(10) returns 60, 0, 80, 0, 85,0, 82, 0, 67,0

C# code:
Byte[] aByte = new Byte[10];
string myStr = "<PURCHASE_ORDER partner=...";
int iByteCount = System.Text.Encoding.Default.GetBytes(myStr, 1,10, aByte,0);

The aBtye returns 80,85,82,67,72,65,83,69,95,79

Firstly, I believe MidB is 1-based rather than 0-based.

Secondly, it looks like it's returning Unicode rather than the default
encoding, so just use Encoding.Unicode instead.
 
M

Morten Wennevik

I'm not familiar with MidB, but what I can see from the documentations it returns a number of characters, and characters in .Net are Unicode. The your default encoding certainly isn't Unicode so do as Jon Skeet said, use Encoding.Unicode.

Hi there,

I have got the following piece of VB code to be translated to C#, I used
System.Text.Encoding.Default.GetBytes(myStr, 1, 10, aBytes, 0) but the result
of aBytes is different from what I have got in VB, please help! Thanks!

VB code:
Dim mystr As String
mystr = "<PURCHASE_ORDER partner=..."
Dim aByte() As Byte
ReDim aByte(10)
aByte = MidB(mystr, 1, 10)

The aByte(10) returns 60, 0, 80, 0, 85,0, 82, 0, 67,0

C# code:
Byte[] aByte = new Byte[10];
string myStr = "<PURCHASE_ORDER partner=...";
int iByteCount = System.Text.Encoding.Default.GetBytes(myStr, 1,10, aByte,0);

The aBtye returns 80,85,82,67,72,65,83,69,95,79
 
A

AstroDrabb

using System.Text

// this will return a byte[] containin
// 60, 0, 80, 0, 85, 0, 82, ..
string tmp = "<PURCHASE_ORDER partner=..."

UnicodeEncoding encoding = new UnicodeEncoding()
byte[] by = encoding.GetBytes(tmp)

// or you could use this which returns
// 60, 80, 85, 82, ..
ASCIIEncoding encoding = new ASCIIEncoding()
byte[] by = encoding.GetBytes(tmp)
 
G

Guest

Thanks for the solution, it works, but when I put more arguments for
GetBytes as below I got "Conversion buffer overflow", any idea? thanks!

oEncoding.GetBytes("<PURCHASE_ORDER partner=",1,10,aByte,1);
 
J

Jon Skeet [C# MVP]

cloudx said:
Thanks for the solution, it works, but when I put more arguments for
GetBytes as below I got "Conversion buffer overflow", any idea? thanks!

oEncoding.GetBytes("<PURCHASE_ORDER partner=",1,10,aByte,1);

Is there any reason why you need to set up the byte array first rather
than using:

aByte = oEncoding.GetBytes("<....", 1, 10);

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top