Jump to content
Sign in to follow this  
funkey

Is this a Bug? - StringLen() & Hex()

Recommended Posts

funkey

Hello.

Try this please:

Local $iLen = StringLen("00") / 2
ConsoleWrite($iLen & @CRLF)
ConsoleWrite(Hex($iLen, 4) & @CRLF)  ;result is 0 ??
ConsoleWrite(Hex(Int($iLen), 4) & @CRLF)
ConsoleWrite(Hex(1, 4) & @CRLF)

Is this a bug or is it just for me? Without division it works!?


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
BrewManNH

This is what I get when I omit the ,4 in the second consolewrite, which shows that 2/2 in floating point notation is different than 2/2 in integer notation.

1

3FF0000000000000

0001

0001


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
funkey

I found this in the documentation, so I know now why this is as it is (but I don't like it).

Changed: Hex() detects doubles internally and processes them respecting binary format.


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
Mat

What would you rather it do? Truncate the data?

Actually, what it should do is simplify the result of any operations like division, so that double values that are actually integral are cast automatically to integers.

Share this post


Link to post
Share on other sites
Valik

Actually, what it should do is simplify the result of any operations like division, so that double values that are actually integral are cast automatically to integers.

No. That's not a good idea for general usage and may not be the desired behavior some of the time. Also, remember, an Int() cast function exists for a reason and will correctly account for some deviation due to floating point inaccuracies. It's also a performance impediment since detecting whole floating point numbers takes a couple steps and can be error-prone without accounting for representation inaccuracies. Detecting those inaccuracies adds additional steps.

Share this post


Link to post
Share on other sites
BrewManNH

Totally talking out my ass here because I am no programmer, but when doing a division, couldn't you do something equivalent to a Mod(x,y) to see if the number to be returned is a whole number or not? That way you could do FP math or not depending upon the return.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Valik

Division is not a cheap operation. You do not want AutoIt to do a mod, conditional test and then possibly a second division. If a user is so concerned about getting a floating point result then they should pre-cast their values or cast the result.

Remember, it's AutoIt's job to make your life easier but it can't read your mind and judge your intent. Sometimes it needs help and that's why we provide cast functions so you gain fine control over what happens.

Share this post


Link to post
Share on other sites
funkey

Next problem:

How can I do this right?

$sData = "0x43E77762000004180101"

$iDB = Int(BinaryMid($sData, 7, 2))
ConsoleWrite($iDB & @CR)        ;wrong

$sDB = BinaryMid($sData, 7, 2)
ConsoleWrite($sDB & @CR)
ConsoleWrite(Int($sDB) & @CR)   ;wrong

ConsoleWrite(Int("0x0418") & @CR)       ;solution that I want!

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
Valik

Format your input right. Integers are stored as little endian but your input data has them ordered for big-endian.

Here's your example modified to put the data in little-endian format.

Local $sData = "0xE7436277000018040101"

Local $sDB = BinaryMid($sData, 7, 2)
ConsoleWrite($sDB & @CR)
ConsoleWrite(Int($sDB) & @CR)   ;wrong

ConsoleWrite(Int("0x0418") & @CR)      ;solution that I want!

Remember, you are working with raw bits when you use Binary functions.

Share this post


Link to post
Share on other sites
wraithdu

Another solution to get the value you are looking for (whatever its significance in integer form may be):

$sData = "0x43E77762000004180101"

$iDB = Int(String(BinaryMid($sData, 7, 2)))
ConsoleWrite($iDB & @CR)        ;better

Share this post


Link to post
Share on other sites
funkey

Thanks Valik anmd wraithdu.

@Valik: Data comes from big endian machine (PLC), so I cannot change the format.

@wraithdu: Thanks for this solution. I found it out by my own right after posting, but had no time for editing the post.

Now I know that the binary functions do not return strings, they just look like some!


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×