473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert Vb6 to C#

Does anyone know how to convert the following VB6 code to C# code?

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As
Any, Source As Any, ByVal bytes As Long)

Dim sngArray(0) As Single
strString = Chr$(107) & Chr$(62) & Chr(139) & Chr$(65)
CopyMemory sngArray(0), ByVal strString, Len(strString)

After running the code sngArray(0) = 17.40548
Thank You
Peter
Sep 16 '08 #1
4 4621
Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Firstly, I want to confirm that I understand your question correctly. We
are trying to get a System.Single type value by combining four unsigned
char's bit value. If my understanding is incorrect, please feel free to let
me know.

I have found two solutions for this objective in C#:

1.Use the Windows API RtlMoveMemory as you did in VB6. But in this
approach, we need to put our codes in unsafe block to use the pointer. My
codes look like:

class Program
{
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError =
false)]
unsafe static extern void CopyMemory(void* dest, void* src, int size);

static void Main(string[] args)
{
unsafe
{
byte[] byteArray = new byte[] { 107, 62, 139, 65 };
float singleValue = 0f;
float* des = &singleValue;
fixed (byte* src = byteArray)
{
CopyMemory(des, src, 4);
}
Console.WriteLine(singleValue.ToString());
}
}
}

2.Actually, it is not necessary to move memory in this case. Thus a better
way is using the BitConverter class in .NET Framework to convert a byte
array to a single type value. Codes are as follows:

class Program
{
static void Main(string[] args)
{
byte[] s = new byte[] { 107, 62, 139, 65 };
Single test = BitConverter.ToSingle(s, 0);
Console.WriteLine(test.ToString());
}
}

The above two code snippets both give me the value 17.40548. I hope this
will help. Please let me know if you have any future questions or concerns.
I will do my best to help on this.
Best regards,
Ji Zhou (v-****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Best regards,
Ji Zhou
Sep 16 '08 #2

""Ji Zhou [MSFT]"" <v-****@online.microsoft.comwrote in message
news:%2******************@TK2MSFTNGHUB02.phx.gbl.. .
Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

Firstly, I want to confirm that I understand your question correctly. We
are trying to get a System.Single type value by combining four unsigned
char's bit value. If my understanding is incorrect, please feel free to
let
me know.

I have found two solutions for this objective in C#:

1.Use the Windows API RtlMoveMemory as you did in VB6. But in this
approach, we need to put our codes in unsafe block to use the pointer. My
codes look like:

class Program
{
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError =
false)]
unsafe static extern void CopyMemory(void* dest, void* src, int size);

static void Main(string[] args)
{
unsafe
{
byte[] byteArray = new byte[] { 107, 62, 139, 65 };
float singleValue = 0f;
float* des = &singleValue;
fixed (byte* src = byteArray)
{
CopyMemory(des, src, 4);
}
Console.WriteLine(singleValue.ToString());
}
}
}

2.Actually, it is not necessary to move memory in this case. Thus a better
way is using the BitConverter class in .NET Framework to convert a byte
array to a single type value. Codes are as follows:

class Program
{
static void Main(string[] args)
{
byte[] s = new byte[] { 107, 62, 139, 65 };
Single test = BitConverter.ToSingle(s, 0);
Console.WriteLine(test.ToString());
}
}

The above two code snippets both give me the value 17.40548. I hope this
will help. Please let me know if you have any future questions or
concerns.
I will do my best to help on this.
Best regards,
Ji Zhou (v-****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/...tance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Best regards,
Ji Zhou

How come the following does not return the same value as yours and how can I
make it return the same value?

class Program
{
static void Main(string[] args)
{
//byte[] b = new byte[] { 107, 62, 139, 65 };

string s = new string((char)107, 1);
s += new string((char)62, 1);
s += new string((char)139, 1);
s += new string((char)65, 1);

System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] b = ascii.GetBytes(s);
Single test = BitConverter.ToSingle(b, 0);
Console.WriteLine(test.ToString());
}
}
Sep 18 '08 #3
On Thu, 18 Sep 2008 22:22:03 -0700, Peter <cz****@nospam.nospamwrote:
[...]
When I read the string from the Access database in VB6.0 I get the
following
ASCII values
(the string is 1024 bytes long, but each four bytes is a float so for
simplicity sake let's pretend there are only 4 bytes)
107,62,139,65
Note: 139 isn't a valid ASCII value. ASCII only goes up to 127. VB6.0 is
supporting an ANSI code page that goes up to 255.
but if I read the same data using C# and run the following code,

char[] cs = s.ToCharArray();

the cs contains the following { 107, 62, 8249, 65 } // this is what
displays
when I hover the mouse pointer over the cs variable in debug mode.
Because in C#, a char is a 16-bit Unicode value, not the ANSI code page.
The 139 has to be converted to a valid Unicode value, and so it is.

As Jon's pointed out, _maybe_ you can get away with then converting this
Unicode values back to the relevant ANSI encoding and get the bytes that
way. But you would certainly (as he suggests) want to run a test pattern
through that includes all 256 bytes values from 0 to 255, and make sure
that they make it through unscathed.

Pete
Sep 19 '08 #4

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 18 Sep 2008 22:22:03 -0700, Peter <cz****@nospam.nospamwrote:
>[...]
When I read the string from the Access database in VB6.0 I get the
following
ASCII values
(the string is 1024 bytes long, but each four bytes is a float so for
simplicity sake let's pretend there are only 4 bytes)
107,62,139,65

Note: 139 isn't a valid ASCII value. ASCII only goes up to 127. VB6.0 is
supporting an ANSI code page that goes up to 255.
>but if I read the same data using C# and run the following code,

char[] cs = s.ToCharArray();

the cs contains the following { 107, 62, 8249, 65 } // this is what
displays
when I hover the mouse pointer over the cs variable in debug mode.

Because in C#, a char is a 16-bit Unicode value, not the ANSI code page.
The 139 has to be converted to a valid Unicode value, and so it is.

As Jon's pointed out, _maybe_ you can get away with then converting this
Unicode values back to the relevant ANSI encoding and get the bytes that
way. But you would certainly (as he suggests) want to run a test pattern
through that includes all 256 bytes values from 0 to 255, and make sure
that they make it through unscathed.

Pete

Thank you

Encoding.GetEncoding(1252) did the trick, now I can convert the memo field
in Access to varbinary(MAX) field in MS SQL.
Sep 21 '08 #5

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

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.