|
Hi Nic,
Thanks for a great article, it helped me immensely. First of all, I am new to C++ and mainly work on VB.NET. I got a task to do and I really can not make sense of a C++ function I got. The function receives a date in format dd/mm/yy hh:mm:ss and then converts a value like this:
5 Ž„„ A0
into a double.
If there is any chance you could convert the function to vb so that I could understand what needs to happen, I would truly appreciate it. Below is the function.
double __fastcall TR_UBISocket2::TimeStamp_UBI_to_Double(int UBITimestamp)<br />
{<br />
TDateTime * TimeStamp = new TDateTime();<br />
unsigned int TempYear;<br />
unsigned int tmpBab = (unsigned)UBITimestamp;<br />
double retTimeStamp;<br />
<br />
try {<br />
TempYear = (tmpBab & 0xfe000000) >> 25;
<br />
TimeStamp->operator =(RecodeDateTime(*TimeStamp, TempYear + 1940, 1, 1, 0, 0, 0, 0));<br />
TimeStamp->operator +=((double)(tmpBab & 0x01ffffff)/(24*60*60));
<br />
retTimeStamp = TimeStamp->Val;<br />
delete TimeStamp;<br />
return retTimeStamp;<br />
}<br />
catch(...){<br />
R_ADD_LOG_ENTRY("Exception");<br />
try { delete TimeStamp; } catch (...){}<br />
return 0;<br />
}<br />
}
They are out there
|
|
|
|
|
hi tyrone,
sorry for taking so long to respond... i am in the middle of writing my thesis...
i will try some psuedocode for you rather than full on VB as it is not my language of trade and i am not that good at it...
tmpBab would have to be a 32-bit number... so, the first thing that is happening at:
TempYear = (tmpBab & 0xfe000000) >> 25; //Get upper most seven bits
here we are taking the top seven bit (the binary for 0xfe000000 = 11111110000000000000000000000000), and by using the 'bitwise and' operator we are zeroing out all the low-order 3 bytes.
then the '>>25" moves the remaining byte to the begining of the integer, that way we will get a number that is 1 byte long. this number then gets added to '1940', to give us the current date. this would be a system specific thing and somewhere along the line someone decided that epoc for this system is 1940.
the line "TimeStamp->operator =(RecodeDateTime(*TimeStamp, TempYear + 1940, 1, 1, 0, 0, 0, 0)); " simply creates a timestamp object that has the correct year in it... all the 'function' stuff that is going on is just some library calls necessary to create the timestamp (in C/C++ we don't have all the nice builtin functions and data types available in languages such as VB or SQL)...
the same sort of thing is happening in the line "TimeStamp->operator +=((double)(tmpBab & 0x01ffffff)/(24*60*60)); // 1 second = 1/(24*60*60) days ", where they are taking the bottom 24 bits (0x01ffffff=000000001111111111111111111111111) as these give them the number of seconds out of the field passed. this is added to the date and now they have a time stamp...
this is now returned to the calling function...
the big thing to remember is that if you want to 'pull off' a section of an integer then you can use the 'bitwise and' (&) operator to take only those bits you are interested in... if you are taking something from a portion that does not include the lowest order bit, then you need to left-shift the value so that you can place the integer of interest into a variable correctly...
for example, if my number is 75 (dec)= 01001011 (bin)= 4B (hex), and i am interested in the top 4 bits, which my protocol tells me make up the number of nodes in my mini-cluster, then i will need to take the top 4 bits of as follows (psuedo-code-esque VB:
dim myNum as integer<br />
dim newNum as integer<br />
<br />
myNum = 4B (all numbers in C/C++ are expressed either in decimal, octal or hexidecimal, for this sort of stuff we tend to use hexidecimal, but there is no reason why you can't express it as decimal or octal)<br />
<br />
newNum = (myNum & 0x0f0) >> 4 (we have taken the top 4 bits, and moved them down 4 places so it is the correct integer...)
lets pause for a moment to see what would happen if we don't left shift in this case...
myNum & 0x0f0 = 0x40 = 64
and with the left shift we get
myNum & 0x0f0 >> 4 = 0x04 = 4
a significant difference... so if we are only interested in the top 4 bits then the answer is '4' in this case...
if we wanted the lower 4 bits then there would be no need to 'left-shift' at all...
i really hope this has answered your question, and sorry again for taking me so long...
cheers
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Urban Traffic Control systems
|
|
|
|
|
Hi Nik,
Thank you so much for getting back to me. I truly appreciate it.
Thanks for the explenation, I had sleepless nights about this issue but its all coming together now!
Once again, thank you!
Have a great day,
Regards,
Tyrone Smith!
They are out there
|
|
|
|
|
hi tyrone,
i was glad to have been of assistance...
if you need any additional help, and depending on my available time, feel free to contact me...
cheers
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Urban Traffic Control systems
|
|
|
|
|
Could anyone tell me how to read a binary file bit by bit(filename is provided by the user) in VC++ using MFC. I want to see the status of each bit and take some action accordingly.
|
|
|
|
|
the easiest way would be to place each bit into a DWORD , then you would use your bit-wise operators to split off the required sections for processing...
if you know that your data is character based (as in char ), then there is no need to use the DWORD , however i find it easier...
now, it all depends on what your protocol is...
in the article, i used the following protocol:
STX: 1byte | Controller ID: 2bytes | Action: 1byte | ETX: 1byte
let us look only at the "Action" byte...
Bit 7: Controller On|Off, Bit 6: Seconds|Minutes , Bit5-0: Time period for action
we pull off that byte in MC++ by using:
<code>
...
char c;
BinaryReader *br = new BinaryReader(File::OpenRead(filename));
while (true)
{
c = (char) br->ReadByte();
if (c & 0x080)
{
}
if (c & 0x040)
{
}
int tm = c & 0x03f
}
</code>
i hope this helps a little, you really need to become familar with the bit-wise operators and the protocols that you use...
cheers
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Traffic Micro-simulation Tools for Traffic Engineers
|
|
|
|
|
struct sACTION_CMD
{ char nPeriod:6;
char bTime:1;
char bController:1;
};
union sACTION
{
sACTION_CMD Cmd;
char Flat;
};
union sACTION l_sCmdAction;
l_sCmdAction.Flat = 0x0c3;
if
(
(l_sCmdAction.Cmd.bController == 1)
&& (l_sCmdAction.Cmd.bTime == 1)
&& (l_sCmdAction.Cmd.nPeriod == 3)
)
{
return TRUE;
}
else
{
return FALSE;
}
Here you have it, with standart C syntax, not even C++ syntax ! No needs to trick that if you know your lessons by heart Teach me something I don't know instead
Kochise
In Code we trust !
|
|
|
|
|
here you have it, you missed the point,
the aim was to do it in MC++ not standard C or C++, so i did teach you something
not to mention, you have WAY many more lines of code than i do, as you do not need all that 'union' stuff if you are feeding each byte as a stream.
now, of greater importance, why would you have that 'if' statement you use? you do not ultimately know what is coming through the stream! i choose to use as an example a controller that was sent a 3 sec ON signal, however the parser does not know this to be the case at all stages in which case you would have to test for everything separately and THEN act upon the message stream...
anyway,re-read the article and read it for what it is, a way to read binary data in .NET not C/C++
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Traffic Micro-simulation Tools for Traffic Engineers
|
|
|
|
|
...I avoid too much abstraction from the hardware layer, probably because I'm an embedded coder at first. And debugging when you rely on the n-th abstraction layer is harder than from the 'n-1'-th abstration layer (till assembler) !
Yeap, I avoid coding in 'freaking' languages invented just to fits coders' wills, instead them to understand the pretty basis of a language such C/C++. I don't even speak about assembler, as everyone spit on it as being the worst language ever.
I see many 'evoluated' (and scriptable) language flowering around there, from 'basic' languages such C and else, to .NET, Java, Perl, Python, ... Plus numerous frameworks such MFC, STL, ATL, ... In a while, you have the choice between a dozen of APIs just to perform the same task !
Is that 'really' necessary to create so much confusion in the world of coding by multiplying the available languages, instead of multiplying the lines of code ? You'll say it's a matter of taste : sure it is !
Now it's not your fault you're messing with .NET, a technology created by MicroSoft just to compete with Java. MicroSoft wanted not to pay a licence fee to someone else (Sun) so they made up .NET ! Java would have been enough to perform the tasks of maintening a web site, now you have .NET ! Welcome to head-aches when it comes to read the code of someone else, when you're used to .NET and the foreign code is in Java.
Coders that 'speaks' more languages will be paid more, not those who writes more and better lines of codes, that's where we are all going : speaking nonsense ! Remember something named 'Babylon' ? It's not God who makes us speaking different programming languages, it would be Gates !
So well, for my example, the test was just here to demonstrate how to fetch informations from the '0x0C3' byte with a struct. I would have benn able to write it down in a different way :
struct sACTION_CMD
{ char nPeriod:6;
char bTime:1;
char bController:1;
};
sACTION_CMD* l_psCmdAction;
char l_nCmdAction = 0x0C3;
l_psCmdAction = (sACTION_CMD*) &l_nCmdAction;
if
(
(l_psCmdAction->bController == 1)
&& (l_psCmdAction->bTime == 1)
&& (l_psCmdAction->nPeriod == 3)
)
{
return TRUE;
}
else
{
return FALSE;
}
Without UNION
And yes, I learned then something from your article, another way to handle bits, cooool !
Kochise
In Code we trust !
|
|
|
|
|
ultimately, one wouldn't use .NET in a hard real time environment, agreed, the reality is that most programmers wouldn't use goto/label either because they have been told that it is bad programming practice, and yet, when i built my finite state machine that is *exactly* what i used because as a long time programmer, i know that there are situations where using a goto/label is the best as long as it is in very concise code and for a specific purpose (i.e. not outside a function bound, and naturally, not making your app one *huge* function that uses unconditional branching...)
however, because i 'removed' the overhead, i can parse on a windows box using XP and .NET 143k and write it out to an output file as formatted csv, in less than one second... not bad for a soft real-time process... i did 100 of those sized files in 68 secs, again, not bad... could it be done faster in asm... absolutely! but the trade-off is that it takes me that much longer to do the same job (assuming i'm an average asm programmer, not an expert) and the gain in minimal...
so, i agree with you, there are many ways to do things, and yes, it is unfortunate that there are so many languages out there... worse yet, we as programmers are forced to learn the latest thing because our management get all excited about it!
ultimately, it comes down to a number of criteria:
1) can i code my project as fast as possible so as to be as productive as possible?
2) will it work on the systems my company are using now and atleast into the near future?
3) can i get the skills when i need them
and that's just the start... i mean it's all about TCO at the end of the day...
as anyone who knows me will testify, i avoid M$ as much as possible, but it can't get in the way of being professional...
as for me moving to .NET, there is a very good reason... if i were still in a company working as a senior a/p, as i was before going back to university, i would NEVER have recommended .NET, in fact i was in the process of moving everyone from C/C++ to Java, however, i work with a group of traffic engineers, these people are expert in their field, but they are not programmers... so .NET gives them an opportunity to use my work; which is still predominately in C/C++ (albeit MC++) and some C# because i refuse to use VB if i can help it...
but these people use VB, that's all they know... so .NET provides us with the ability to directly share functionality, even though they will never understand my C/C++ code, and now they can be more productive... see?
and as programmers, we generally know not to rely on Windows for anything (except to blue screen) and i certainly wouldn't do what BMW's engineers have done, and that is use Windows CE for the OS in the cars they build... imagine travelling down the autobahn at 200kmph and suddenly your dash decides to blue screen? i would have used one of the MANY stable RTOS that are commercially available, and you could write everything in C and ASM to ensure that the thing worked in hard real-time...
long story short, it comes down to "Nik's Law"
"the right machine, with the right OS, developing using the right language, at the right price for the job at hand"
and that job can be as long term and short term as you choose...
cheers
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Traffic Micro-simulation Tools for Traffic Engineers
|
|
|
|
|
I know the story, as they were promised the coders will code faster, produce more applications, and managers gets profit percentage and career advancement, but not coders :/
Well, do ya know a little bit about "Murphy's laws" ? Try these links : http://dmawww.epfl.ch/roso.mosaic/dm/murphy.html and http://www.murphys-laws.com/ !
How much time do we need to learn another language just to make things working together or make our managers happy ? It would mean we will be students forever, unable to expert any language ! Knowing at least one language by heart would solve any problem you may encounter.
Now, as you have to link .NET with VB with stuff and so : here is the problem ! Industry creates new standarts every years instead to let coders practice one common language for good !
I remember a good ol'day when there was 20 of platforms each running one operating system ! Now we have one platform (PC) running 20 operating systems/languages ! Is that really better, in the name of compatibility ?
The discuss could last, and it's an interresting one If you wanna try something quite neat, try QNX 6.2.1 = POSIX, GCC, ... real-time, hence quite easy to code !
Kochise
In Code we trust !
|
|
|
|
|
my friend, i agree with you 100%
and naturally this is the problem with our industry... i made mention of this at a presentation i made recently, where i said that if we used the same principles in building bridges or buildings as we do on software, nothing would stand up!
ultimately, with all these 'simpler' languages, what we have bred is a class of programmer that doesn't really understand how everything works, because they don't need to know how everything works!
even if all they learnt was C, at least they would appreciate that if you allocate memory, you must free memory... all this GC rubbish is breeding lazy programmers!
you and i, it seems, share the same passion, and that's good to see and know!
as i mentioned though, there are times when we just don't have a choice, and we do things because we have to, not because we want to... that is what makes us professionals...
may i ask though... can i make calls across from QNX 6.2.1 to Linux? this is important as my databases are currently on LINUX and i actually do need to move to a RTOS... and as a heads up for you, there is a new OS being developed called Mungi... this will be eventually a RTOS, but it is also a single address space OS, which means that if you add more machines to it, it treats it like one HUGE machine rather than a whole lot of little small ones connected...
cheers
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Traffic Micro-simulation Tools for Traffic Engineers
|
|
|
|
|
Check out at http://www.qnx.com !
The new QNX version 6.3 is about to be released in may or june 2004 : http://www.qnx.com/developers/6_3_glance.html
You may also download the current version, the 6.2.1, follow this link and read carefully the tips I give : http://www.osnews.com/phorum/read.php?f=10&i=1049&t=1044#reply_1049
Now to explain things a bit : QNX is NOT Unix or Linux. QNX is POSIX compliant, so does Unix and Linux. QNX have a very strong X-Windows emulation layer which makes easy to port Linux apps to QNX. But the best is to code in native QNX APIs
Once you have installed QNX 6.2.1, you'll get the complete MSDN-like documentation in the help viewer.
The coding environment is a bit nasty though : command-line compiler (GCC 2.95) and you may have to create your own makefiles
You can also use WorkSpace : http://pages.infinit.net/micbel/ ! It ease a lot the coding procedure
The best is Eclipse from IBM : http://www.eclipse.org/ ! Download the lastest QNX release here : ftp://download2.eclipse.org/R-2.1.2-200311030802/eclipse-SDK-2.1.2-qnx-photon.zip !
There is not real unzipper into QNX, so unzip the QPR file somewhere on a FAT32 partition QNX could reach, boot under QNX, and install the package with the QNX Installer Eclipse is a bit slow to start, don't worry
Kochise
PS : http://www.openqnx.com/ - http://www.qnxzone.com/
PS2 : BeOS 6 aka Zeta -> http://www.yellowtab.com/ - http://www.zetajournal.org/ - http://www.zetanews.com/ - http://www.bebits.com/ (free BeOS apps)
In Code we trust !
|
|
|
|
|
sorry i didn't get back sooner!
thanks for the information, i have now downloaded it, and will try installing in the next few weeks...
thanks again
nik
Nik Vogiatzis
PhD Candidate: University of South Australia
+++++++++++++++++++++++++++
Developing new generation Traffic Micro-simulation Tools for Traffic Engineers
|
|
|
|
|