I am reading a file on my computer that contains the following information:
cellcount=011 (INT)
currentdensity=1.112 (REAL)
REAL2=2.1145 (REAL)
INT1=41823 (INT)
REAL3=4.2023 (REAL)
INT=11 (INT)
Currently I am storing the ReadBuffer in a string(1000) because I thought that was the easiest way to manipulate the content. I want to be able to extract the numbers as you see and store them in variables. I want it to be dynamic so folks can enter any number (not reals in to ints, but otherwise).
so far I have looked at the string functions of twincat 3 and using MID() and FIND() I can make something work, but then I need to know the length of the numbers. Like this:
test.CellCount := STRING_TO_INT(MID(sTest,number_of_chars,FIND(sTest,'cellcount:')+10));
Any idea how to make this dynamic?
Square brackets after a string variable will allow you to extract the ASCII code of a particular character. Knowing that digits 0-9 are ASCII codes 48-57, you can iterate through the characters following your search string until no more digits are found. For example:
loc1 := FIND(sTest,'cellcount=') + 9;
FOR i:=loc1 TO (loc1+10) DO // 10 = maximum length of number
IF (sTest[i]>=48 AND sTest[i]<=57) OR sTest[i]=46 THEN
loc2 := i;
ELSE
EXIT;
END_IF
END_FOR
number_of_chars := loc2 - loc1 + 1;
ASCII code 46 is the decimal point, to allow parsing of floating point values.
Related
I'm pulling in data that is in long hexadecimal string form which I need to convert into decimal notation, truncate 18 decimal places, and then serve up in JSON.
For example I may have the hex string:
"0x00000000000000000000000000000000000000000000d3c21bcecceda1000000"
At first I was attempting to use ParseUint(), however since the highest it supports is int64, my number ends up being way too big.
This example after conversion and truncation results in 10^6.
However there are instances where this number can be up to 10^12 (meaning pre truncation 10^30!).
What is the best strategy to attack this?
Use math/big for working with numbers larger than 64 bits.
From the Int.SetString example:
s := "d3c21bcecceda1000000"
i := new(big.Int)
i.SetString(s, 16)
fmt.Println(i)
https://play.golang.org/p/vf31ce93vA
The math/big types also support the encoding.TextMarshaler and fmt.Scanner interfaces.
For example
i := new(big.Int)
fmt.Sscan("0x000000d3c21bcecceda1000000", i)
Or
i := new(big.Int)
fmt.Sscanf("0x000000d3c21bcecceda1000000", "0x%x", i)
I'm having some issues on trying to convert an int to hex then, padding it with 0s in order to get a 6 Characters String which represents the hex number.
So far, I tried the following:
intNumber := 12
hexNumber := strconv.FormatInt(intNumber, 16) //not working
And then I found out how to pad it with 0s, using %06d, number/string. It makes all the strings 6 characters long.
Here you can Find a Playground which I set up to make some tests.
How can I achieve this in a efficient way?
For any Clarifications on the question, just leave a comment below.
Thanks In advance.
import "fmt"
hex := fmt.Sprintf("%06x", num)
The x means hexadecimal, the 6 means 6 digits, the 0 means left-pad with zeros and the % starts the whole sequence.
I have this variable:
danumber := "542353242"
and want to extract a character from the string and operate with it as a number.
Tried this:
int(danumber[0])
but it doesn't seem to work.
What your expression gives you is the character code for the digit. To convert the character to the character's value, subtract 0's character code from it:
int(danumber[0] - '0') // in your example, this is: 53 - 48
If you want to convert multiple digits, I would recommend using the strconv package:
number, err := strconv.Atoi(danumber[0:2]) // convert first two characters to int
4I must write strings to a binary MIDI file. The standard requires one to know the length of the string in bytes. As I want to write for mobile as well I cannot use AnsiString, which was a good way to ensure that the string was a one-byte string. That simplified things. I tested the following code:
TByte = array of Byte;
function TForm3.convertSB (arg: string): TByte;
var
i: Int32;
begin
Label1.Text := (SizeOf (Char));
for i := Low (arg) to High (arg) do
begin
label1.Text := label1.Text + ' ' + IntToStr (Ord (arg [i]));
end;
end; // convert SB //
convertSB ('MThd');
It returns 2 77 84 104 100 (as label text) in Windows as well as Android. Does this mean that Delphi treats strings by default as UTF-8? This would greatly simplify things but I couldn't find it in the help. And what is the best way to convert this to an array of bytes? Read each character and test whether it is 1, 2 or 4 bytes and allocate this space in the array? For converting back to a character: just read the array of bytes until a byte is encountered < 128?
Delphi strings are encoded internally as UTF-16. There was a big clue in the fact that SizeOf(Char) is 2.
The reason that all your characters had ordinal in the ASCII range is that UTF-16 extends ASCII in the sense that characters 0 to 127, in the ASCII range, have the same ordinal value in UTF-16. And all your characters are ASCII characters.
That said, you do not need to worry about the internal storage. You simply convert between string and byte array using the TEncoding class. For instance, to convert to UTF-8 you write:
bytes := TEncoding.UTF8.GetBytes(str);
And in the opposite direction:
str := TEncoding.UTF8.GetString(bytes);
The class supports many other encodings, as described in the documentation. It's not clear from the question which encoding you are need to use. Hopefully you can work the rest out from here.
I'm reading in a lot of lines of hex data. They come in as strings and I parse them for line_codes which tell me what to do with the rest of the data. One line sets a most significant word of an address (MSW), another line sets the least significant (LSW).
I then need to concatenate those together such that if MSW = "00ff" and LSW = "f10a"
address would be 00fff10a.
This all went fine, but then I was supposed to check if address was between a certain set of values:
if address <= "007FFFh" and address >= "000200h" then
print "I'm in"
end
As you all probably know, Lua is not a fan of this as it gives me an error using <= and >= with strings.
If there a way I can convert the string into hex, such that "FFFF" would become 0xFFFF?
You use tonumber:
local someHexString = "03FFACB"
local someNumber = tonumber(someHexString, 16)
Note that numbers are not in hexadecimal. Nor are they in decimal, octal, or anything else. They're just numbers. The number 0xFF is the same number as 255. "FF" and "255" are string representations of the same number.