Number to String conversion function in ABAP - abap

I want to show a message of type E for which I have to first create a string. The string has mixed string and integer variables to be joined.
Since only strings can be concatenated, I copy integer variable into string variable, make a whole string and concatenate.
Is there a conversion function such as to_string(integer_variable) that can convert integers to string?
PROGRAM abc.
DATA: im_acc_no TYPE i VALUE 100,
lv_acc_no TYPE string,
lv_msg TYPE string.
START-OF-SELECTION.
lv_acc_no = im_acc_no.
CONCATENATE 'Acnt# ' lv_acc_no ' does not exist' INTO lv_msg.
MESSAGE lv_msg TYPE 'E'.

There is the CONV operator (SAP help) which can do something similar to to_string but it is not allowed in the CONCATENATE, so won't help you in your scenario.
You could use the && operator (SAP help) to create the message in-place in the MESSAGE command like:
MESSAGE |Acnt# | && lv_acc_no && | does not exist| type 'E'.
Side note: do not use this variant of the MESSAGE command, it might be easy to program but it makes it hard to investigate where a message is being generated. For this reason it is better to actually create a message in SE91 and use that. Variable replacements (&) in the message also handle integers just fine.

Related

How could BRO-IDS compare strings with NUL-terminator

I am testing string comparison with BRO, and got some runtime errors. Hope you guys could take a look and give me some hints.
For example i have two strings, let's say str_A and str_B, str_A is sort of a pattern, like: str_A = "\x13\x02\xf0\x80";
And str_B is a payload(contents) string from the function:
event tcp_packet(c: connection, is_orig: bool, flags: string, seq: count, ack: count, len: count, contents: string)
I compared the two of the strings with: if(str_A in str_B), which reduced the runtime errors like:
1467860547.182543 error: string with embedded NUL: "\x13\x00\xf0\x13"
1467860547.182543 error: string without NUL terminator: "\x13\x00\xf0\x13\x02\xf0\x80\x02\x00\x00\xc0\x01\x00\x00\x00\x00\x87\x02"
It looks like the 'x00' in the middle of the pattern string was considered as a terminator, and for the latter there wasn't a NUL at the end of the str_B.
So the (silly) question is how i could append a NUL at the end of str_B within BRO? and how to make BRO ignore the embeded NUL in the middle of a string when comparing? Many Thanks.
This was figured all right by translating(calling the function string_to_ascii_hex()) the hex-string into an ASCII-hex-string.

Labview converting hexformatted string to ascii

In labview I am trying to convert a hex string to ascii format. For example if I have a hexstring like: 09124E4F21CD0024FFFFFFFFFFFFFFFF the ascii version of this is : NO!Í or basically a bunch of illegible symbols. I tried using the labview functions of converting hexstring to number but they didn't work. How would I convert the ascii part to hexformatted ascii?
Hexadecimal String to Number works fine, but only for a hex string that represents a number that can be stored as a numeric data type:
If the input string represents a number outside the range of the
representation of number, number is set to the maximum value for that
data type.
Your example input is 128 bits long whereas the longest integer data type in current LabVIEW is 64 bits.
You can use this function, but you need to convert the input one byte at a time:
Create a While Loop and add a shift register. Initialise the shift register with your input string.
Inside the loop, wire the string from the shift register to the string input of a Search/Split String function
Wire a numeric constant of 2 to the offset input - i.e. split the string into the first two characters, and the rest
Wire the match + rest of string output to the right-hand shift register terminal
Wire the substring before match output to a Hexadecimal String to Number function
Wire the default input of this function to a numeric constant with value 0 and type U8
Wire the output of this function to the right-hand side of the While loop and make the terminal indexing (via right-click)
Use an Empty String/Path? function to exit the While loop when the string being passed back into the shift register is empty.
The output from the indexing terminal you created will now be a U8 (byte) array containing the data converted from the input string. If you want it in string form you can convert it using Byte Array to String.
This assumes that your input string is always a multiple of 2 characters in length. If you need it to handle an input such as "3F2" you'll need to check for this and do something to the input (I'll let you figure out what) before passing it into your loop.

SAS: Why does the input function convert from character to numeric, and the put function convert from numeric to character?

SAS documentation defines the input and put functions as:
Input function: Returns the value that is produced when SAS converts an expression using the specified informat
Put function: Returns a value using a specified format.
So the input function takes the variable and an informat as arguments, while the put functions takes the variable and a format as arguments, right?
If that is the case, why is the input function used to convert a variable from character to numeric, while the put function is used to convert a variable from numeric to character?
Are the input and put functions more tied to informats and formats, respectively, as opposed to character to numeric and numeric to character conversions, respectively?
Also, what is the difference between the input and put functions and the input and put statements?
The last question answers the first one. It is easiest to think of the PUT() function and INPUT() function in terms of how the PUT and INPUT statements work.
The PUT statement is for printing data into a text file report. You can print numeric or character variables but you are always writing character strings. The INPUT statement is for reading data from a text file. You can read into numeric or character variables but you are always reading from character strings.

change display format for all string variables to as short as possible

After compressing my data, I have several string variables with storage type str4 or str1 and format %9s. I would like to revert them all to the default display format, which help dformat reports should be %#s for str#. Is there a quick way to do this?
This is the structure of my best guess:
ds, has(type string)
foreach v of varlist `r(varlist)' {
format `v'
}
This does not work because, instead of reformatting to the default value with this command, the format function just displays the format.
A reproducible example:
clear
input str50 mystr
"b"
"a"
end
compress
format myst
This is the situation I was confronted with. I am not sure if it applies beyond strL formatted variables. (Roberto suspects that it does not; see comments.)
Addendum. My goal here was to make browse-ing my data easier. It seems that format is respected in the browser (truncating to length of one for %1s, say), while it is overridden by the actual length of the string when printing to the console.
I am surprised that you seem surprised, as with your syntax the format command (not function) does indeed just display the format, as is documented. Incidentally, you don't need a loop to do that as format will take a varlist as argument:
. clear
. set obs 1
obs was 0, now 1
. foreach t in a b c {
2. gen `t' = "`t'"
3. }
. format a b c
variable name display format
-----------------------------
a %9s
b %9s
c %9s
-----------------------------
Joshing apart, I think you need just one line here, which is something like
. format a b c %1s
or
. format a b c %-1s
to signal which justification. Stata doesn't truncate displayed strings just because they don't match the string display format; it might well truncate strings because there isn't space to show them, but (I'm naturally open to counter-examples) the above display formats for variables will do well for most purposes.
EDIT: The following device may help.
gen length = 0
ds, has(type string)
quietly foreach v in `r(varlist)' {
replace length = length(`v')
su length, meanonly
format `v' %`r(max)'s
}
drop length

FORTRAN 90 - Input Syntax Error

This should be an easy one.. I can't figure out why my read statement has a syntax error. I have a file 7477 lines long and I want each of those variables to correspond in each line like my format specifies. Any help here would be great. Thanks!
implicit none
integer :: spe, flen = 7477, i
real, dimension (7477):: wnum,s,A,abh
character :: other
integer :: lun = 11
write(*,*) 'Opening File!'
open(lun,file ='h2o_allbands',status = 'old',action ='read')
write(*,*) 'Success!'
17 format (1x,i2,3x,F9.6,1x,E9.3,1x,E9.3,F5.5,A120)
do i = 1, 7477
read(lun,17) spe(i),wnum(i),s(i),A(i),abh(i),other
write(*,*) wnum(i)
end do
The read has spe(i) as an input list item. spe is not declared as an array, so the compiler probably thinks spe(i) is a reference to an integer function. You cannot read "into" the result of a plain integer function.
Perhaps spe should be declared as an array?
Without seeing a line from your input file, it is difficult to say what the exact problem is: However:
First of all, you should not use a format statement when reading entities (unless in special cases), as this can lead to all sort of different errors, if your line is not well formatted for whatever reasons. So just replace the read line with:
read(lun,*) spe(i), wnum(i), s(i), A(i), abh(i), other
If all the lines are read in well apart the last one, then make sure, that you have a newline at the end of the last line.