- Taufan Lubis – Ubuntu Linux
- Be free…. be yourself with Linux.
- Linux Programming with Assembly Language – Convert ASCII string to Number
- Thread: Shell script: convert string to number
- Shell script: convert string to number
- strtol(3) — Linux man page
- Synopsis
- Description
- Return Value
- Errors
- Conforming To
- Notes
- Example
- Linux string to number one
- [SOLVED] Converting string to number in bash
- Convert a String to a Number — UNIX
- Argument string to integer in bash — Unix & Linux Stack .
- C String to Int: Simple Ways to Convert to Numeric Values .
- AWK: convert string to numeric, how? | Howtoforge — Linux .
- How to convert String to Numeric in sql
- Java Syntax: Converting Strings to Numbers
- converting string to number in shell script — UNIX
- How to Convert String to Number in JavaScript
- Oracle / PLSQL: TO_NUMBER Function — TechOnTheNet
- strsignal(3): return string describing signal — Linux man page
- How to Compare Numbers and Strings in Linux Shell Script
- How to Use the strings Command on Linux
- Bash split string into array using 4 simple methods .
- How to use grep command in UNIX / Linux
- Chmod Numeric Permissions Notation UNIX / Linux Command .
- strings(1) — Linux manual page
- How to find a string or text in a file on Linux — Linux .
- Linux/Unix: Cat Command Display Line Numbers — nixCraft
- linux — How to test if a variable is a number in Bash — By .
Taufan Lubis – Ubuntu Linux
Be free…. be yourself with Linux.
Linux Programming with Assembly Language – Convert ASCII string to Number
November 25, 2018 by taufanlubis
In assembly, register only know ASCII (American Standard Code for Information Interchange) characters. It mean, If you are working with number and need to display the result by calling syscall number 4 (32 bit x86), you can’t just put the result address into ecx register. Because, the ecx register will assume that your result in Hex number.
For example, your result is 7. ecx will assume that it’s 7hexadesimal not 7decimal meanwhile 7 is 37hexadesimal in ASCII table.
Let’s try in coding to see how it works.
It’s very simple program. I just do simple math, 4+2=6. I add 4 into eax register and 2 into ebx register. Then add ebx register to eax register, so the result will be:
eax = eax + ebx.
= 4 + 2
= 6
But why, the result is not ‘6′ as I expected.
Let’s use gnu debugger to explore the code. You have to re-compile again with option -gstabs for enable the debugger function.
List down the codes so we can set where is the break start.
Set break on line 5.
Set, assembly format to Intel instead of the default, AT&T.
Run the program
Check the current instruction.
Check register eax, ebx and ecx. It’s still empty
Go to next instruction and check the register again.
Check current instruction position.
Go to next instruction and check the register.
Check position of current instruction.
Go to next intruction and check the registers. Ebx register already added into eax register.
Content of eax is moved to memory then from memory is moved to ecx register.
Now, let’s see the content of ecx register.
It’s not ‘6‘ is ‘\006‘, that’s why the result is wrong.
Why it happens? Because ecx register will assume that it is a character not value.
If you check in ASCII table, 6 hex is ACK (acknowledge) which is can not be displayed.
So, how we convert ascii into number?
If you see ASCII table below:
character → hex number
0 → 30
1 → 31
2 → 32
3 → 33
4 → 34
5 → 35
6 → 36
7 → 37
8 → 38
9 → 39
If I want to convert character ‘6‘ into number ‘6‘, I just need to add ‘30‘, so it will become 36 in hexadecimal number or ‘6‘ in character.
So, if we go back to our code, we just add ‘30‘ before we send to ecx character to ‘result’ memory variable.
Well, it works now.
Check again with use gnu debugger again to see how it work.
Just set break to 9.
Run the program and disassemble it.
Check the registers.
Go to next intruction and check the registers. Eax register now has 0x36 or 36hexadecimal or ‘6‘ in character. What is ‘54‘, it’s decimal of 36 hexadecimal. We are talking about character now not value.
Go to next instruction few times until instruction pointer is at 0x080480a3.
Check the string character in ecx register. It’s ‘6‘. So it’s correct.
Источник
Thread: Shell script: convert string to number
Thread Tools
Display
Shell script: convert string to number
I’m very new to shell scripting in Bash.
I’m trying to figure out how to treat the output of stat -c %s filename as a number, and not as a string. If you run that command, it will output the size of the file in bytes.
Here’s the script I’m working on. It iterates over every file in the working directory, and attempts to print the cumulative size of all the files over which it iterated so far.
The problem should be evident from the output:
/Pictures$ ./print-cumulative-sizes.sh
1036228+0
970325+1036228+0
1108950+970325+1036228+0
1476318+1108950+970325+1036228+0
1027836+1476318+1108950+970325+1036228+0
1050479+1027836+1476318+1108950+970325+1036228+0
1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
1215533+1156703+1050479+1027836+1476318+1108950+97 0325+1036228+0
960132+1215533+1156703+1050479+1027836+1476318+110 8950+970325+1036228+0
1569659+960132+1215533+1156703+1050479+1027836+147 6318+1108950+970325+1036228+0
1149074+1569659+960132+1215533+1156703+1050479+102 7836+1476318+1108950+970325+1036228+0
4096+1149074+1569659+960132+1215533+1156703+105047 9+1027836+1476318+1108950+970325+1036228+0
98528+4096+1149074+1569659+960132+1215533+1156703+ 1050479+1027836+1476318+1108950+970325+1036228+0
142+98528+4096+1149074+1569659+960132+1215533+1156 703+1050479+1027836+1476318+1108950+970325+1036228 +0
142+142+98528+4096+1149074+1569659+960132+1215533+ 1156703+1050479+1027836+1476318+1108950+970325+103 6228+0
See what’s wrong? How can I get bash to treat my $SIZE and $TOTAL variables as numbers, not as strings?
Thank you in advance for any help!
Источник
strtol(3) — Linux man page
strtol, strtoll, strtoq — convert a string to a long integer
Synopsis
Description
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional ‘+’ or ‘-‘ sign. If base is zero or 16, the string may then include a «0x» prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is ‘0’, in which case it is taken as 8 (octal).
The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter ‘A’ in either upper or lower case represents 10, ‘B’ represents 11, and so forth, with ‘Z’ representing 35.)
If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not ‘\0’ but **endptr is ‘\0’ on return, the entire string is valid.
The strtoll() function works just like the strtol() function but returns a long long integer value.
Return Value
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).
Errors
(not in C99) The given base contains an unsupported value.
The resulting value was out of range. The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned).
Conforming To
strtol() conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and strtoll() to C99 and POSIX.1-2001.
Notes
Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero value after the call.
According to POSIX.1-2001, in locales other than the «C» and «POSIX», these functions may accept other, implementation-defined numeric strings.
BSD also has with completely analogous definition. Depending on the wordsize of the current architecture, this may be equivalent to strtoll() or to strtol().
Example
The program shown below demonstrates the use of strtol(). The first command-line argument specifies a string from which strtol() should parse a number. The second (optional) argument specifies the base to be used for the conversion. (This argument is converted to numeric form using atoi(3), a function that performs no error checking and has a simpler interface than strtol().) Some examples of the results produced by this program are the following:
Источник
Linux string to number one
Sep 06, 2021 · Converting string variables to numbers. Let’s declare some «numbers» which are basically strings of characters. [email protected]:
$ b=3. You can check the assigned number: [email protected]:
$ echo $a 11 [email protected]:
[SOLVED] Converting string to number in bash
Oct 07, 2012 · Converting string to integer in BASH: pgb205: Programming: 17: 08-31-2010 08:38 PM: converting string in integer in bash script: dziadgba: Linux — Newbie: 5: 08-31-2009 05:59 PM [Bash] increment number in a string: czezz: Programming: 4: 07-01-2009 11:34 AM: Getting a number out of a string in bash: Luc484: Programming: 5: 01-20-2006 02:15 AM: converting string to asterisks: pantera
Convert a String to a Number — UNIX
Mar 17, 2017 · echo «Number = $number» # Number = 3. number=three. echo «Number = $number» # Number = 0. # Tries to evaluate the string «three» as an integer. Certain arithmetic operations are permitted for declared integer variables without the need for expr or let. n=6/3. echo «n = $n» # n = 6/3. declare -i n. n=6/3.
Argument string to integer in bash — Unix & Linux Stack .
Sep 27, 2015 · To perform arithmetic, you should invoke the arithmetic expansion operator $ ( (. )). For example: $ a=2 $ echo «$a + 1» 2 + 1 $ echo «$ ( ($a + 1))» 3. or generally preferred: $ echo «$ ( (a + 1))» 3. You should be aware that bash (as opposed to ksh93, zsh or yash) only performs integer arithmetic.
C String to Int: Simple Ways to Convert to Numeric Values .
May 27, 2014 · #include int main() < unsigned char text[]="1234"; int integerValue; sscanf(text, "%d", &integerValue); printf("Integer value is: %d", integerValue); return0; > Example 4: A program to convert a string to an integer using the strtol() function. The C strlol() function converts a string to a long integer. This is a 64-bit integer.
AWK: convert string to numeric, how? | Howtoforge — Linux .
Jun 09, 2008 · isLoad15=$ (uptime | awk ‘ < print $NF >‘) maxLoad15=2.00 if [ «$isLoad15» gt «$maxLoad15» ] then …. into a numeric, as required for the «gt — condition». this would mean I’d have to do it all in AWK, but I’d need the var in the shell script, as there are several existing scripts which only need the «isLoad15» value.
How to convert String to Numeric in sql
How to convert String to Numeric. To convert a String to Numeric uses sql conversion functions like cast or convert. Syntax. CAST ( expression AS datatype [ ( length ) ] ) CONVERT ( datatype [ ( length ) ] , expression [ , style ] ) Examples. SELECT CAST(‘127’ AS NUMERIC); SELECT CAST(‘127.86’ AS …
Java Syntax: Converting Strings to Numbers
Jul 03, 2019 · Converting Numbers to Strings. To make a number into a String follows the same sort of pattern as the String class has a valueOf method too. It can take any of the primitive data type numbers as an argument and produce a String: int numberTwenty = 20; String converted = String…
converting string to number in shell script — UNIX
Feb 22, 2012 · Concatenate a string and number and compare that with another string in awk script I have below code inside my awk script if ( $0
How to Convert String to Number in JavaScript
The first argument of parseInt must be a string. parseInt will return an integer found in the beginning of the string. Remember, that only the first number in the string will be returned. For example when we have the string “384.75a” and we want to convert it to a number, the result will be 384.
Oracle / PLSQL: TO_NUMBER Function — TechOnTheNet
The syntax for the TO_NUMBER function in Oracle/PLSQL is: TO_NUMBER( string1 [, format_mask] [, nls_language] ) Parameters or Arguments string1 The string that will be converted to a number. format_mask Optional. This is the format that will be used to convert string1 to a number…
strsignal(3): return string describing signal — Linux man page
- Synopsis
- Description
- Return Value
- strsignal():
1. Since glibc 2.10: 2. _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L 3. Before glibc 2.10: 4. _GNU_SOURCE
See more on linux.die.net
How to Compare Numbers and Strings in Linux Shell Script
Sep 13, 2020 · In this tutorial on Linux bash shell scripting, we are going to learn how to compare numbers, strings and files in shell script using if statement. Comparisons in a script are very useful & after comparison result, script will execute the commands and we …
How to Use the strings Command on Linux
Jul 09, 2019 · To ask strings to use two as the minimum length, use the following command. strings -n 2 jibber. We now have two-letter strings included in the results. Note that spaces are counted as a printable character. Piping strings Through Less. Because of the length of the output from strings, we’re going to pipe it through less. We can then scroll through the file looking for text of interest.
Bash split string into array using 4 simple methods .
# /tmp/split-string.sh My array: string1 string2 string3 Number of elements in the array: 1. To overcome this we convert and split string into array. Now your variable can have strings or integers or some special characters, so depending upon your requirement you can choose different methods to convert string …
How to use grep command in UNIX / Linux
Chmod Numeric Permissions Notation UNIX / Linux Command .
Dec 18, 2009 · C an you provide more information about chmod command octal mode number notation? The command chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits. chmod never changes the permissions of symbolic links; the chmod system call …
strings(1) — Linux manual page
Strings can be configured so that this option is the default behaviour. In such cases the -a option can be used to avoid using the BFD library and instead just print all of the strings found in the file. -f—print-file-name Print the name of the file before each string.
How to find a string or text in a file on Linux — Linux .
Aug 17, 2020 · On a Linux system, the need to search one or multiple files for a specific text string can arise quite often.On the command line, the grep command has this function covered very well, but you’ll need to know the basics of how to use it. On GUI, most text editors also have the ability to search for a particular string. In this article, we’ll show you how to quickly and easily search through .
Linux/Unix: Cat Command Display Line Numbers — nixCraft
Feb 03, 2013 · cat —number foo.c We can use the more command/less command as filter when text can not be fitted on the screen: cat —number foo.c | more cat —number foo.c | less. Displaying line number using cat command. The -b / —number-nonblank option number all nonempty output lines, starting with one and the syntax is: cat -b fileNameHere OR
linux — How to test if a variable is a number in Bash — By .
This rejects empty strings and strings containing non-digits, accepting everything else. Negative or floating-point numbers need some additional work. . How to test if a variable is a number in Bash linux red hat debian opensuse ubuntu arch linux mandrake get link linux computer linux pc linux server linux desktop learn linux red hat linux .
Источник