Groovy string integer range - string

How can use the range operator on string representing integers?
The real problem arise when a string represents an integer bigger than 9. The range will consider the string as a list of numbers, and only use the first one instead of converting the whole string as an integer.
String start = '1'
String end = '11'
println "Range over strings"
(start..end).each{println it}
println "Range over integers"
(start.toInteger()..end.toInteger()).each{println it}
Result:
Range over strings
1
Range over integers
1
2
3
4
5
6
7
8
9
10
11
I would like to keep the code simple and avoid if possible using too much type conversions since I need the resulting list to contain numbers as string.

You'll need to do type conversions, maybe a custom range is an idea:
class CustomRange extends IntRange {
CustomRange(String start, String end) {
super(start.toInteger(), end.toInteger())
}
}

Related

Python: setting a string so it always has 2 decimals after comma

I'm looking to write a Python function that adds a number to the back of the string. However, I want it in a way that the string always has 2 characters after the comma.
I believe using a string is easier to remove and skip characters. I will be converting the result with the float() method.
As an example:
I start at the string "0.00"
Adding a 5 will make it "0.05"
Adding a 5 and a 6 will make it "5.56" etc
Another example:
again we start at "0.00". Adding consecutive the characters "5" "4" "3" "2" "1" will ultimately result in "543.21"
Why not just convert the string to an integer and divide the number by 100?
num = int(input())
print(num/float(100))
E.g. input = '5',
convert to integer = 5,
Divide by 100 = 0.05

By creating a Variable that is defined as a string with a fixed-length, is the used bytes per cell or range?

as a relatively new user to Excel, I could not seem to find any confirmation if the a string with a fixed-length has the memory assigned per range or cell.
I am thinking it is per range, because I could not create a string with a fixed-length and set the range as the last cell in a row.
Ex:
Dim HilvlActivity as String * 3
HilvlActivitySource = Range("F3", "F:F").End(xlDown).Row
And instead, had to use
Dim HilvlActivity as String * 5000
HilvlActivitySource = Range("F3", "F:F").End(xlDown).Row
So my question is basically: is the assigned fixed-length definition per cell (Ex: F3) or per the entire assigned range?
I may be overthinking this, or should have coded the end of the row more efficiently (will change later). But this is still a basic concept that I want to make sure I understand. Some of the information I have looked into is John Walkenbach's book for Power Programming with VBA, Microsoft (https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/string-data-type), PowerSpreadSheets (https://powerspreadsheets.com/vba-data-types/#Long-VBA-Data-Type), but still can't seem to find the correct answer.
Anyone know of any good resources that really dives into variable details, it would be appreciate. Otherwise, thanks for the help! :)
Well, first: You haven't defined HilvlActivitySource as a variable anywhere...
The length of a fixed-length-string is applied to the string variable itself. For example:
Dim HilvlActivity as String * 3
HilvlActivity = ActiveSheet.Range("F3").Value
MsgBox Len(HilvlActivity)
will always show the message 3 - if F3 contains less than 3 characters, then there will be spaces added to the end. If F3 contains more than 3 characters, then only the first 3 will be stored.
Dim a as String * 10 means that the string a will be always of length 10 and if an assigned value goes above this length it will be cut to the first 10 chars.
This illustrates it:
Public Sub TestMe()
Dim a As String * 3
Dim b As String
Dim c As String * 10
a = "ABCD"
b = "ABCD"
c = "ABCD"
Debug.Print a 'ABC
Debug.Print b 'ABCD
Debug.Print Len(c) '10
Range("A1") = StrReverse(c) ' DCBA in range "A1" with 6 empty spaces upfront
End Sub

TI-Basic String to number

Essentially, my code returns a string which only contains a 1 or 2 digit integer, such as "8" or "10". Is there any way to convert this to an actual number, such as one that could be then stored in a list or matrix?
Assuming the string is in Str1, you can simply do this:
expr(Str1->X
This will evaluate whatever crazy math is inside of Str1 (in this case, it's just a straight up number) and store it into X.

Excel : Find only Hexa decimals from 1 cell

I'm a newbie on Excel.
So I have a list of some names ending with Hexa decimals. And some names, that doesn't have any.
My mission is to see only those names with Hexa decimals. (Mabye somehow filter them out)
Column:
BFAXSPOINTDEVBAUHOFLAN2AD
BFAXSQLBAUHOFLAN207
BFAXSQLDEVBAUHOFLAN27A
BFREPDEVBAUHOFLAN258
BFREPORTINGBAUHOFLAN20B
COBALTSEA02900
COBALTSEAVHOST900
DIRECTO8000
DIRECTO9000
DIRECTODCDIRECTOLA009
DYNAMAEBSSISE006
SURVEYEBSSISE006
KVMSRV00",
KVMSRV01",
KVMSRV02",
ASR
CACTI
DBSYNC",
DTV
and so on...
The Function HEX2DEC will help you achieve what you want - it attempts to convert a number as a hexidecimal, into a decimal. If it is not a valid Hex input, it will produce an error.
The key is understanding how many digits you expect your decimal to be - is it the last 5 characters; the last 10; etc. Also note that there is a risk that random text / numbers will be seen as hexidecimal when really that's not what it represents [but that's a problem with the question as you have laid it out; going solely based on the text provided, all we can see is whether a particular cell creates a valid Hexidecimal].
The full formula would look like this[assuming your data starts in A1, and that your Hexidecimal numbers are expected to be 6 characters long, this goes in B1 and is copied down]:
=ISERROR(HEX2DEC(RIGHT(A1,6)))
This takes the 6 rightmost characters of a cell, and attempts to convert it from Hex to Decimal. If it fails, it will produce TRUE [because of ISERROR]; if it succeeds, it will produce FALSE.
Then simply filter on your column to see the subset of results you care about.
Consider the following UDF:
Public Function EndsInHex(r As Range) As Boolean
Dim s As String, CH As String
s = r(1).Text
CH = Right(s, 1)
If CH Like "[A-F]" Or CH Like "[0-9]" Then
EndsInHex = True
Else
EndsInHex = False
End If
End Function
For the string to end in a hex, the last character must be a hex.

Conversion from hexadecimal string to Double yields wrong results

I am trying to convert 14 bit hex numbers to decimal.
I have this VBA code.
Option Explicit
Public Function HexadecimalToDecimal(HexValue As String) As Double
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = CDec(ModifiedHexValue)
End Function
With numbers like this to convert to decimal
0x047B1142591E80
0x044A81325A1E80
0x047B7542591E80
I keep getting random results across large amounts of data. Sometimes spot on other times the numbers are off by 6 or 2.
Try changing the return type of the function from Double to Variant. Double has only about 15 decimal digits of precision, so can't, for example, capture the value 1261213964639872 (which has 16 digits) exactly. The closest it can get is 1261213964639870. By changing the return type to Variant, the full precision returned by CDec will be preserved. You can't use a Decimal return type, because VBA for some reason does not support this.
The problem isn't with VBA. Excel cells can only hold 15 digits in number format. So the "number" 1234567891234567 will always display 1234567891234560. This can be avoided by converting items to text AND/OR changing the cell format to text.
But this doesn't always work.
The only surefire way to make sure it will retain all digits is to append something onto the string that isn't a number.
This code will append an apostrophe before the number, but return the entire string.
Public Function HexadecimalToDecimal(HexValue As String) As String
Dim ModifiedHexValue As String
ModifiedHexValue = Replace(HexValue, "0x", "&H")
HexadecimalToDecimal = "'" & CDec(ModifiedHexValue)
End Function
Unfortunately, not a perfect solution.

Resources