473,385 Members | 1,311 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,385 software developers and data experts.

MidB in VB vs GetBytes in C#?

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

Nov 17 '05 #1
5 6269
cloudx <cl****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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.

On Tue, 10 May 2005 22:48:15 +0200, cloudx <cl****@discussions.microsoft.com> wrote:
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


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3
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)
---
Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
Nov 17 '05 #4
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);
"AstroDrabb" wrote:
using System.Text;

// this will return a byte[] containing
// 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);
---
Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/

Nov 17 '05 #5
cloudx <cl****@discussions.microsoft.com> wrote:
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);

?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

0
by: Tiraman | last post by:
Hi , I have a Stored Procedure that return an xml (by using the xml auto or explicit) in to some XmlReader and i m getting the following error . Invalid attempt to GetBytes on column ''....
2
by: Gaurav Sharma | last post by:
Hi guys, As most of you must be knowing that people normally used MidB, InstrB, lenB functions till VB6 for binary string manipulations.Since now these functions are not available in VB.NET, I...
2
by: Dan | last post by:
I'm storing a byte array in a varbinary field in a SQL Server 2000 database, eg: dim b() as byte = new byte() {1,2,3,4} but when I attempt to retrieve the byte array (using...
16
by: les | last post by:
I'm trying to http post xml data to a remote server and I get this error: BC30518: Overload resolution failed because no accessible 'GetBytes' can be called with these arguments: Line 20: Dim...
1
by: Emilio | last post by:
Question about Dim data As () = System.Text.Encoding.ASCII.GetBytes(message) Would it be the same to write: Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(message) ?
1
by: devgrt | last post by:
Checking if proper/best way to do this... I have to pass a C# string to a C++ DLL. The function in the DLL (gethostbyname) requires a null terminated string. It seems like when you do byte var =...
0
by: Alex | last post by:
As most of you must be knowing that people normally used MidB, InstrB, lenB functions till VB6 for binary string manipulations.Since now these functions are not available in VB.NET, I want to...
2
by: gizmo | last post by:
Hi, Here's a little hack I put together to try to get to the bottom of a problem I'm having with trying to base64 encode a hash value. The hash value contains character codes 135 and 130...
5
by: DaveD | last post by:
Can anyone help me get this compiled ? void Write<T>(T val) { byte bytes = BitConverter.GetBytes(val); Array.Reverse(bytes); writer.Write(bytes); } The problem is that for T=bool,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.