How to decode base64 & hexadecimal response from query - elrond

The query comes as
"returnData": [
"zWCLtKpUXZbkWNM9deAVPizTxXASOjX63ubdUHDN+vw=",
"zWCLtKpUXZbkWNM9deAVPizTxXASOjX63ubdUHDN+vw="
],
How can I decode that string? I can see it is decodable base64 first, but I get a very weird string after. Can someone show me the steps to follow to receive the decoded final string?

Base64 encodes binary data, which means if you send data that is not a string you won't be able to retrieve a string back. So to properly parse the return data you will need to know what data types were actually returned.
It might also be helpful to use a base64 to hex decoder first, so you get the hex representation of your data.
To give you more concrete recommendations we would need to know what data you expected, preferably showing the whole endpoint definition as well as any custom structs that might be involved.

Related

How does GitHub encode their graphQL cursors?

GitHub's graphql cursors are intentionally opaque, so they shouldn't ever be decode by a client. However I'd like to know their approach towards pagination, especially when combined with sorting.
There are multiple layers of encoding for the encoding used for pagination cursors used by GitHub. I will list them in order from the perspective of a decoder:
The cursor string is encoded using URL safe base64 meaning it uses - and _ instead of + and /. This might be to have consistency with their REST based API.
Decoding the base64 string gives us another string in the format of cursor:v2:[something] so the next step is decoding the something.
The 'something' is a binary encoded piece of data containing the actual cursor properties. The first byte defines the cursor type:
0x91 => We don't use any sorting, the cursor contains the length of the id field and the id itself. 0xcd seems to indicate a two-byte id, 0xce a four-byte id. This is followed by the id itself, which can be verified by decoding the base64 id graphql field.
0x92 => A composite cursor containing the sorted property and the id. This is either a length-prefixed ordinal number or two bytes plus a string or ISO date string followed by the length-prefixed id.

CapnProto encodes string twice

I have a simple struct containing some stuff, and also a Text field. I was looking at the result of encoding this data using Capnp, and for some reason the value of the text field appears in the encoded output twice! That doesn't seem very efficient or sane. Why does this happen?
Cap'n Proto does not encode text fields twice. To understand what happened in your case, we'd need to see your code.

Difference between String Value and Binary Value in Registry

Can someone explain me when to use String Value or Binary Value in windows registry? Is there any security concerns also attached with these or not ?
I want to store date in encrypted format
You must use the binary format. Encrypted data cannot be stored in a string. It will randomly get corrupted when the string is normalized, not every byte value is a valid Unicode codepoint. If you absolutely want a string then you have to encode the data, Convert.ToBase64String().

Convert base64 byte array to an image

I have a form bean with attributes id, desc and imageByteArray. Struts action gets executed and it redirects to a JSP where i want to access these bean attributes like id, desc and convert the imageByteArray and display it as an image. I tried this post, but that's not working for me.
I encode the bytearray using Base64 - where this.bean.imageByteArray refers to the form bean
this.bean.setImageByteArray(new org.apache.commons.codec.binary.Base64().encode(imageInByteArr));
I tried this, but not working
<img src="data:image/jpg;base64,<c:out value='${bean.imageByteArray}'/>" />
Byte array (byte[] imageByteArray) refers a base64 encoded JPG image and I'm getting the following img tag as output and obviously nothing gets displayed,
<img src="data:image/jpg;base64,[B#2e200e">
Any idea how to convert base64 byte array and display as an image in JSP?
What you get is just the toString output of an array. You need however the byte array converted to a String.
You should create a method in bean
public String getByteArrayString()
{
return new String(this.imageByteArray);
}
and reference this in your JSP.
While technically you should define which encoding to use for an array of base64 bytes this is not necessary as all characters are in the standard 7bit ASCII range.
DoubleMalt's answer (accepted at the time of writing) is unfortunate, because it's sort of using two wrongs to make a right. It doesn't help that Apache Commons Codec makes it so easy to do the wrong thing :(
Base64 is fundamentally an encoding from binary data to text - as such, it should almost always be used to convert a byte[] to a String. Your issue is that you're converting a byte[] to another byte[] - but you later want to use that data as a string. It would be better to convert once, in the right way.
Now you can choose exactly when you convert to base64 (and a string). You could do it early, in your Java code, in which case I'd use:
// Obviously you'd need to introduce a new method for this, replacing
// setImageByteArray
this.bean.setImageBase64(new Base64().encodeToString(imageInByteArr));
<img src="data:image/jpg;base64,<c:out value='${bean.imageBase64}'/>" />
Alternatively, you could keep just the binary data in your bean, and the perform the encoding in the JSP. It's been a long time since I've written any JSPs, so I'm not going to try to write the code for that here.
But basically, you need to decide whether your bean should keep the original binary data as a byte[], or the base64-encoded data as a String. Anything else is misleading, IMO.

How to SMS string that base on Base64

HI,
anything different between string and string that base on Base64? I can send out string by SMS programmaticallly. But not sure this will apply to string that is base on Base64. can someone provide guidance what need to be done on the string base on Base64 and send it by SMS. Thanks
Do you mean you want the text message to just contain the base64 string itself? That should work fine - as far as I'm aware, all the characters used within base64 are also available in text messages. If you mean you want to send arbitrary binary data which you happen to have in base64 form at the moment, that could be harder. It wouldn't really be "text" at that point - I dare say there's a way of sending it (possibly MMS etc) but you should be looking at APIs which take byte arrays rather than strings at that point.
have you tried to send a Base64String via SMS?
EDIT... thanks to Jon :)
At the risk of another downvote by the almighty Jon Skeet ;) I will provide "some" help... #MilkBottle, if you have a byte[] and you convert it (for example with Convert.ToBase64String) you can send the result as a smsbody. I cannot understand, why you want to do this, but that doesn`t matter. Another (bad) example:
byte[] arr = new byte[] {0x02, 0x04, 0x07};
String smsbody = System.Convert.ToBase64String(arr);
The result will look like this "AgQG" and this is able to be send programmatically with the SmsComposerTask.
Hope that helps...

Resources