473,769 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB->C conversion need help with formating function

140 New Member
Hi,

I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine.

I've been moving along at a nice pace, but this conversion has stumped me.

I need a function to take two arguments:(a double number which may or may not be an integer, and an integer which will be the number of digits to store after the decimal place) it needs to turn this into a string which is always 10 characters long. if the number is less than 10 characters, it would be leading spaces. Here is the VB code:
Expand|Select|Wrap|Line Numbers
  1. Private Function Format10(ByVal dblNumber As Double, ByVal intDecimal As Integer) As String
  2.         Format10 = CStr(FormatNumber(dblNumber, intDecimal, , , TriState.False)).PadLeft(10)
  3.     End Function
  4.  
Some examples of input/output: ('b'=space)
Format10(100.0, 4) -> "bb100.0000 "
Format10(100.0, 0) -> "bbbbbbb100 "
Format10(100.33 33, 2) -> "bbbb100.34 "

I was working towards using fprintf's (and %fX.Y), to come up with my formating, but couldn't figure out how to send that formating to a char-array(string). If it helps, every time the function is used its related to a fprintf line, so I also toyed with just formating a the variables at out printing time, but can figure out how to tack on the leading spaces which would keep it at 10 characters. If I was doing this in VB (besides using the above function, I would of figured out the length of the number, and add spaces to the beginning until the total length of the string = 10...and am having trouble shaking this algorithm from my head...

I'm definitely never going to take VB.net for granted, now that I've been working on this conversion... :)
Thanks,
Sitko.
Oct 29 '07 #1
2 1748
Banfa
9,065 Recognized Expert Moderator Expert
how about using sprintf instead of fprintf?
Oct 29 '07 #2
sitko
140 New Member
Yeah, I was looking into sprintf, which for some reason, wasn't mentioned as a potential alternative to printf/fprintf, from my sources...

But, I think I've figured it out (haven't ran it yet):

I did a bit more analysis, and found that the second number (numofdigits is set to 0 in nearly every use of this function, and is set to 2 or 3 only twice in the code, so I did it like this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10d\n";
  2. fprintf(datFile, str, intIndex);
  3.  
Which even is less code than the other way. When there are multiple calls, in one line I do this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10d%10d         0\n";
  2. fprintf(datFile, str, (10000*intIndex+1),(intIndex*100+1));
  3.  
When its set to 2 or 3 I'll just hard code whichever one it is, like this:
Expand|Select|Wrap|Line Numbers
  1. char * str = "%10.2f\n";
  2. fprintf(datFile, str, <doublenumber>);
  3.  
Thanks, for your help, I'll let the forum know if this works when I get to test this...
Sitko.
Oct 29 '07 #3

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

Similar topics

5
2626
by: Colin MacDougall | last post by:
Hi, I am just starting to learn Visual Basic 6 and was wondering if just going straight on to VB.NET would be a better bet. Is VB.NET just a newer version of Visual Basic 6 or does it differ radically? My main reason for programming in VB is to build a data logger that reads, processes and stores bytes from an external device through the serial port and then exports it to an
19
2164
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and cannot in other? And finally why C# seems to be a sort of standard in industry? Regards, Raj Dhrolia.
22
2562
by: ByteSize | last post by:
Dear All, Please, this is not meant to be offensive - but it is a challenge !!! I have posted on over a dozen so called 'vb.net' expert / blog sites - in the vain hope of finding a complete, accurate and ACTUALLY FUNCTIONING snippet to demonsrate a successful api call to CreateProcess() using VB.NET. So far NO ONE has taken up the challenge - not even MSDN or MS personnel. I have code that works in vb6, I have dilligently tried to...
3
1358
by: Philippe TEIRUH | last post by:
Hello, I have made a migration from VB 6.0 to VB.NET and noticed a great speed change when I access (from VB) to Excel Cells (same problem to write Visio formulas from VB). The same code takes 21 seconds in VB 6.0 and 54 seconds in VB.NET (see below) 1. Is that fact well known? 2. How to solve the problem? 3. Can we hope amelioration in latter version?
132
5790
by: Kevin | last post by:
I don't know if I should even start this topic but here goes. I'm an ex vb6 developer, now developing in C#. The reason why I started developing in C# is because the company that I worked for at the time standarised on C#. Many of my friends working in previous companies that I worked for are starting to move back to VB.Net. When I asked them why, it seems that the next release of VB.Net seems very promising and they kinda see...
9
3127
by: Stanley | last post by:
I have a class "clsABC" which is created by VB.NET. I am trying use it in the C# program. Although I can add it in the project reference, I can't browse the methods and properties in the coding. Please see the following coding: using clsABC clsABC tmpClass = new clsABC(); tmpClass. <------ I can see any methods and properties.
4
1507
by: Kannan | last post by:
Hi, We are planning to migrate one VB project to .Net. But I have a common questions. can any one clarify my below questions. 1. Is there is any advantage of using C# than VB.net. I feel both are same in terms of performance, security and etc., Is it correct? 2. I feel that converting from VB to VB.net is much more easier than VB to C#. Is it correct?
4
1478
by: Chris | last post by:
I am looking at this code in C# Assembly a=Assembly.LoadFrom("employee.dll"); Type t=a.GetType("Company.Employee"); I can't figure out what it is in VB.net can anyone help. I try Dim login_assembly = Assembly.LoadFile(Server.MapPath("bin/login.dll")) Dim login_type As Type = login_assembly.GetType("clsLogin")
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7392
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3947
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 we have to send another system
3
2810
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.