Convert rawstring (Byte String) into readable String - abap

Requirement:
To convert SRT_MMASTER table's MESSAGE_DATA field data into readable string format or in internal table.
I have tried different function modules to convert Byte String (Blob) data stored in SRT_MMASTER table's MESSAGE_DATA field but none of it returned readable string format or at least generate an XML file in return.
I have tried Function modules like :
SCMS_BINARY_TO_STRING,
SDIXML_XML_TO_DOM,
SDIXML_DOM_TO_DATA,
SMUM_XML_PARSE
and methods like :
cl_soap_xml_parser=>get_data
cl_soap_xml_parser=>get_formatted_data
CALL METHOD cl_bcs_convert=>raw_to_string
and more, but none were able to convert it to correct readable format.
Can you suggest which function module or class/method can be used to solve the purpose?

You can use IMPORT from DATA BUFFER and cl_soap_xml_helper=>xstring_to_string
DATA:
lt_message_item TYPE srt_persistency_item_t,
lx_message_data TYPE xstring.
SELECT SINGLE message_data FROM srt_mmaster INTO lx_message_data.
IMPORT message_data = lt_message_item FROM DATA BUFFER lx_message_data.
DATA(lv_string) = cl_soap_xml_helper=>xstring_to_string( lt_item[ 1 ]-value ).

Related

What to try to get BigQuery to CAST BYTES to STRING?

BigQuery Standard SQL documentation suggests that BYTE fields can be coerced into STRINGS.
We have a byte field that is the result of SHA256 hashing a field using BigQuery itself.
We now want to coerce it to a STRING, yet when we run "CAST(field_name to STRING)" we get an error:
Query Failed Error: Invalid cast of bytes to UTF8 string
What is preventing us from getting a string from this byte field? Is it surmountable? If so, what is the solution?
Below example should show you an idea
#standardSQL
WITH t AS (
SELECT SHA256('abc') x
)
SELECT x, TO_BASE64(x)
FROM t
in short - you can use TO_BASE64() for this
If you want to see the "traditional" representation of the hash in String, you have to use TO_HEX() function.
WITH table AS (
SELECT SHA256('abc') as bytes_field
)
SELECT bytes_field, TO_HEX(bytes_field) as string_field
FROM table
By default in the UI, BigQuery shows you the base64 representation but if you want to compare it with other sha256 function from other language, for example, you have to use TO_HEX()
You can try SAFE_CONVERT_BYTES_TO_STRING() function.
reference: SAFE_CONVERT_BYTES_TO_STRING

Converting string into xstring without using function module

I want to convert a string into a xstring. I know that there is a function module named "SCMS_STRING_TO_XSTRING"
But since it is not a good habit to use function modules anymore, a class based solution would be my prefered way to go.
I know that there is a class
cl_abap_conv_in_ce
but I can only validate, that this class can convert xstrings into string. I wand to have the reverse case. Does anyone have experience on how to do that class based?
Meanwhile, I found the solution on my own. For people who might be interested:
DATA(lo_conv) = cl_abap_conv_out_ce=>create( ).
lo_conv->write( data = lv_content ).
DATA(lv_xstring) = lo_conv->get_buffer( ).
The help text for XSTRING provides a nice functional method for this:
cl_abap_codepage=>convert_to( )
Firstly, you need to decide how you want it encoded. UTF-8? UTF-16? Just plain HEX?
For UTF-8 You can do the following using system calls (instead of function calls):
First do a global once-off initialization:
STATICS: g_conv_utf8 TYPE xstring. " used for conversion
DATA: l_flags TYPE c LENGTH 1.
system-call convert id 20
srcenc 'SET LOCALE LANGUAGE'
dstenc 'UTF-8'
replacement '#'
type l_flags
cinfo g_conv_utf8.
And then do subsequent calls: l_string -> l_xstring (+ l_len)
SYSTEM-CALL CONVERT ID 24
DATA l_string
ENDIAN ' '
IGNORE_CERR 'X'
N -1
BUFFER l_xstring
LEN l_length
CINFO g_conv_utf_8.
This is the essence of what cl_abap_codepage=>convert_to( ) does internally.

Get field names from a TFRecord

Given a .tfrecord file, we can define a record iterator
record_iterator = tf.python_io.tf_record_iterator(path=record)
Then parse it using
example = tf.train.SequenceExample()
for element in record_iterator:
example.ParseFromString(element)
The question is: how do we infer all the field names in the context ?
If we know the structure in advance, we can say example.context.feature["width"]. In addition, str(example.context) returns a string with the entire record structure. However, I was wondering if there is any built-in function to get the field names and avoid parsing this string (e.g. by searching for "key")

Postgres: How to store bytea as value in hstore?

I am trying to store a bytea as a value in hstore and I keep getting the following error:
function hstore(unknown, bytea) does not exist
Here is what I have tried:
UPDATE users set store = store || hstore('key1', pgp_pub_encrypt('testval',dearmor('xxxx')));
am trying to store a bytea as a value in hstore
You cannot safely store bytea in hstore without some kind of encoding, because hstore is stored as text in the current text encoding, but bytea has no text encoding and may also contain null bytes that are not legal in text strings.
So you need to encode the bytea as hex, base64, or some other form that makes it into valid text in the current encoding.
The simplest way is to cast it to text, but I recommend instead using encode and decode to/from base64. This encoding is more compact than the text or hex encodings used for bytea's text representation in PostgreSQL, and it's also independent of the value of the bytea_output setting.
e.g.
UPDATE users
SET store = store
|| hstore('key1', encode( pgp_pub_encrypt('testval',dearmor('xxxx'))), 'base64')
WHERE ... ;
There are two variants of the hstore() function taking arrays, one taking a row / record and one taking two text values. The last one is for you:
SELECT hstore('foo','abc'::bytea::text); -- cast bytea to text!
Your attempt failed the rules of function type resolution because there is no explicit cast registered from bytea to text. Postgres falls back to input / output conversion for that, which does not count for function type resolution:
Is there a way to disable function overloading in Postgres
So:
UPDATE users
SET store = store
|| hstore('key1', pgp_pub_encrypt('testval',dearmor('xxxx'))::text)
WHERE ... ;

How to retrieve data from data_type RAW in Oracle with Groovy?

I have a table in oracle db with data type RAW.
I would like export into an xml file but when I retrieve the data from the RAW column I get a [#4r5... instead of the value in db (123454678...)
How can I write this value in xml (the goal is to export in another db)
Thanks a lot.
You don't say what code you're using to access the column, but it looks like you are getting back a byte[]
You should be able to print the actual bytes in groovy using:
println obj.toList().join(',')
But if you were expecting a string or something, you'll need to know how it was converted to bytes in the first place...
If you want the byte array converted to an array of integers, you can just do:
List byteList = a.toList()
(or if you want your bytes to be treated as unsigned, you can do)
List byteList = a.collect { it & 0xff }
It depends what you intend to do with the array though... It is probably best left as an array...
Did you try PL/SQL?
declare
a mytable.rawcol%TYPE;
begin
select rawcol into a from mytable;
-- now do something with "a"
end;