Jump to content



Photo

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


  • Please log in to reply
10 replies to this topic

#1 funkey

funkey

    New Dad

  • Active Members
  • PipPipPipPipPipPip
  • 485 posts

Posted 22 February 2012 - 07:22 PM

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.








#2 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 22 February 2012 - 07:39 PM

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


How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#3 funkey

funkey

    New Dad

  • Active Members
  • PipPipPipPipPipPip
  • 485 posts

Posted 22 February 2012 - 08:38 PM

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.


#4 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,042 posts

Posted 22 February 2012 - 08:53 PM

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.

I don't know where I'm going, but I'm on my way.


#5 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 22 February 2012 - 09:15 PM

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.

#6 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 22 February 2012 - 09:24 PM

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.

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#7 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 22 February 2012 - 09:28 PM

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.

#8 funkey

funkey

    New Dad

  • Active Members
  • PipPipPipPipPipPip
  • 485 posts

Posted 24 February 2012 - 06:51 PM

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.


#9 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 24 February 2012 - 08:26 PM

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.

#10 wraithdu

wraithdu

    I am less fun than a twisted ankle.

  • MVPs
  • 2,137 posts

Posted 24 February 2012 - 08:36 PM

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


#11 funkey

funkey

    New Dad

  • Active Members
  • PipPipPipPipPipPip
  • 485 posts

Posted 25 February 2012 - 06:34 PM

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users