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

VB to C# convertion problem

122 100+
The following line is giving me problems. The end result should be value = -1749281834 on the first pass, I am not sure how to convert this to c# seems the ways I have tried are really messing up and don't return the right values.

VB:

Expand|Select|Wrap|Line Numbers
  1.  
  2. value = value Or ((lngSign < 0) And 1) Or (CBool(lngSign And &H40000000) And &H80000000)
  3.  
value has a current value of : 1272842731 (before this line executes)
lngSign has a value of : 1073741824
&H40000000 is = 1073741824
&H80000000 is = -2147483648

original VB code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Function LongLeftRotate(ByRef value As Integer, ByRef Bits As Integer) As Integer
  3.         Dim lngSign, lngI As Integer
  4.         Bits = Bits Mod 32
  5.         If Bits = 0 Then LongLeftRotate = value : Exit Function
  6.         For lngI = 1 To Bits
  7.             lngSign = value And &HC0000000
  8.             value = Int(value And &H3FFFFFFF) * 2
  9.             value = value Or ((lngSign < 0) And 1) Or (CBool(lngSign And &H40000000) And &H80000000)
  10.         Next 
  11.         LongLeftRotate = value
  12.     End Function
  13.  
Apr 30 '10 #1
6 3568
Monomachus
127 Expert 100+
@DragonLord
Using Convert VB.NET to C# - A free code conversion tool

I have something like this:
Expand|Select|Wrap|Line Numbers
  1. public int LongLeftRotate(ref int value, ref int Bits)
  2.         {
  3.             int lngSign = 0;
  4.             int lngI = 0;
  5.             Bits = Bits % 32;
  6.             if (Bits == 0) { return value; }
  7.  
  8.             for (lngI = 1; lngI <= Bits; lngI++)
  9.             {
  10.                 lngSign = Convert.ToInt32(value & 0xc0000000);
  11.                 value = Convert.ToInt32(value & 0x3fffffff) * 2;
  12.                 value = value | ((lngSign < 0) && 1) | ((bool)lngSign & 0x40000000 & 0x80000000);
  13.             }
  14.  
  15.             return value;
  16.         }
  17.  
  18.  
Apr 30 '10 #2
Plater
7,872 Expert 4TB
Not quite sure what that line does, you apply a mask to a boolean value?
Expand|Select|Wrap|Line Numbers
  1.  
  2. value = value | ((lngSign < 0) & 1) | (((bool)(lngSign & 0x40000000)) & 0x80000000) 
  3.  
  4.  
This will always produce a zero: (((bool)(lngSign & 0x40000000)) & 0x80000000)
VB really shouldn't even let you apply a bitmask to a boolean value, C# probably wouldn't
Apr 30 '10 #3
DragonLord
122 100+
@Plater
yes exactly that is my problem it behaves differently in the VB than it does in the C#

i get zero all the time so it always uses the value...i need it to not do so.. i tried i the same thing that Monomachus suggested however i get errors that you can't cast type int to type bool and cannot apply '&&' operator to type int and bool

Anyone else have any suggestions.. I am trying to avoid using the visualbasic dll reference and convert the application fully to C# and so far this is the only line i can tell that is standing in my way.
May 1 '10 #4
DragonLord
122 100+
The best guess that it is doing a bit shift to the left and rotate. This all new to me so I AM not sure if I an saying it right. Any
thoughts????
May 1 '10 #5
Plater
7,872 Expert 4TB
Hmm well it should always be zero in VB as well.

This operation:
lngSign And &H40000000
produces either a zero or 0x40000000
Converting that to a bool, should either produce a false(for zero) or true(for the other value i suppose)
Applying a mask of 0x80000000 after the fact is pointless even if you didn't cbool it.
0x80000000 & 0x40000000 will always produce a zero.

Now if the intent was to see it the 0x80000000 AND the 0x40000000 bit are set on the value of lngSign you could do this:
(lngSign & 0xC0000000)
//Which produces either a zero or 0xC0000000
//Which if you turn into a bool NOW its either true/false depending on if BOTH bits are set. Which is pobably what the VB is trying to do.
Something similar to this could be used
bool a =( (lngSign & 0xC0000000) >0);
May 3 '10 #6
DragonLord
122 100+
Funny enough here is how i solved it today....

Expand|Select|Wrap|Line Numbers
  1.  
  2.  private Int32 LongLeftRotate(ref Int32 value,int Bits)
  3.         {
  4.             long lngSign;
  5.             long lngI;
  6.             int temp;
  7.             Bits %= 32;            
  8.             if (Bits == 0)
  9.             {
  10.                 return (value);
  11.             }
  12.  
  13.             for (lngI = 1; lngI <= Bits; lngI++)
  14.             {
  15.                 lngSign = value & -1073741824;
  16.                 value = (value & 0x3FFFFFFF)*2;
  17.                 temp = value;
  18.                 bool b;
  19.                 b = (lngSign < 0);
  20.                 int l;
  21.                 if (b == true)
  22.                     l = 1;
  23.                 else
  24.                     l = 0;
  25.  
  26.                 if (lngSign < 0 && lngSign!= -1073741824)
  27.                 {
  28.                     value = (value + l);
  29.                 }
  30.                 if (lngSign == 0x40000000)
  31.                 {
  32.                     value = value + Int32.MinValue;
  33.                 }
  34.                 if (lngSign == -1073741824)
  35.                 {
  36.                     value = value + Int32.MinValue +1;
  37.                 }
  38.    //             MessageBox.Show(lngSign + "  " + temp + "  " + value);
  39.               //  MessageBox.Show(value.ToString());
  40.             }
  41.             return (value);
  42.  
  43.  
  44.  
  45.         }
  46.  
  47.  
I have looped through this endlessly and it matches what the VB code is doing every time..... on to tackle the Chr() function lol....be posting for that shortly.
May 4 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Petr Studenovsky | last post by:
Hello I made small GUI test program in Visual Basic NET for SQL communication. I need some help with these problems. problem 1) If I change some settings in datagrid from properties window...
0
by: Konstantin Kivi | last post by:
I have problems getting character set convertion to work in 4.1.4 gamma under linux. Can sombody point me to the online recource other than standard MySQL doc so that I get more information.
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
1
by: Vidar | last post by:
I have a problem in dotNET XSL convertion object. (XMLTRANSFORM) It won't convert UTF-8 to ISO-8859-1 I use this stylesheet for konvertion: <?xml version="1.0" encoding="ISO-8859-1"?> <!--...
0
by: McKoy | last post by:
Hi, I'm translating some code from C++ to C#, but I don't know how to change the code below to get it working under C# ...., I've tried explicity convertion, but without any results :( Please...
4
by: juli | last post by:
Good afternoon! I have an error while trying to convert to DateTime ,here is the code and the error: ArrayList al = new ArrayList(); temp_str=Line.Split(' '); al.Add(temp_str); //...
8
by: juli | last post by:
I am trying to convert an object of a class which is derived from collectionbase class.It's not working: while ((line = file.ReadLine()) != null && line.IndexOf(" ")!=-1) { al.Add(line); }...
4
by: kaikai | last post by:
Hi, I've got a problem while trying to make my source code clean. In a template: template<typename T> class A { public: T data; };
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
4
by: HackerisNewKnight | last post by:
Hello, 1) First problem. In my project i need to convert from char* to BYTE*( BYTE is unsigned char), is there any way to make the convertion? BYTE* bite; char* ch="help me, please"; bite =...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.