Avoid double slashes from json string json-c - c

I tried to encode a byte array using glib2.0. After I add the encoded data to a json object, after I send the encoded data through pubnub client,
I got doble slashed string from json.
This is my code :
//code for encoding data
const guchar *data=inputEncodingData;
encodedData=g_base64_encode (data,datalength);
g_print("Encoded data==>%s\n",encodedData);
It produces encoded string with slashes. like this
KA4IChkAGQAAAAAAAAAAAOAO4A4uDgAAAAAAAAoAGAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQD//wAAwf/A/8D/AAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAEA//8CAP7/BAD6/wgA9P8SAOb/JwA2/A39mP0AAAIA4P/wD939pf2w/eL87vxz
/Nn7zasAAHMKAADIlAkKGAAYAAAAAAAAAAAA4Jbgls6UAAAAAAAACgAYACYAAAAAAAAAyf+9/7//
vv/B/8D/wP+8/77/v//C/8D/vf+//8D/wP++/77/
This is the code for creating json object:
struct json_object *obj1;
obj1 = json_object_new_object();
json_object_object_add(obj1, "result", json_object_new_string(encodedData));
This will print like this:
KA4IChkAGQAAAAAAAAAAAOAO4A4uDgAAAAAAAAoAGAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQD\/\/wAAwf\/A\/8D\/AAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAEA\/\/8CAP7\/BAD6\/wgA9P8SAOb\/JwA2\/A39mP0AAAIA4P\/wD939
pf2w\/eL87vxz\/Nn7zasAAHMKAADIlAkKGAAYAAAAAAAAAAAA4Jbgls6UAAAAAAAACgAYACYAAA
AAAAAAyf+9\/7\/\/vv\/B\/8D\/wP+8\/77\/v\/\/C\/8D\/vf+\/\/8D\/wP++\/77\/
Why is it like this? Any idea? I need data with one slash.

Related

How to convert image file to base64 String in flutter?

I am try to convert a image file in flutter:
File _img=new File('/data/user/0/com.example.test3/app_flutter/2020-10-29T17:18:56.210347.png');
List<int> imageBytes = _img.readAsBytesSync();
String imageB64 = base64Encode(imageBytes);
print(imageB64);
But it look like is a wrong base64 String and I cannot decode to image on convert website:
https://codebeautify.org/base64-to-image-converter
iVBORw0KGgoAAAANSUhEUgAAAhwAAALECAYAAABDk+k+AAAAAXNSR0IArs4c6QAAAARzQklUCAgICHwIZIgAACAASURBVHic7d1ngFx1ucDhd9N77z0hbdMhCSAJVZoURS4C0psColcxFBXFKypKUywUQYp0EdtVQHpHTQLpZZOQnpDee9v7QeUSksxsyP7Ptuf5tnPeybwfCPvLmTNnIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgNJRUFYvXFxcXFxWrw0AVVVBQUGZ/O6vVhYvCgBULYIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhMcAEByggMASE5wAADJCQ4AIDnBAQAkJzgAgOQEBwCQnOAAAJITHABAcoIDAEhOcAAAyQkOACA5wQEAJCc4AIDkBAcAkJzgAACSExwAQHKCAwBITnAAAMkJDgAgOcEBACQnOACA5AQHAJCc4AAAkhM
Is the dart base64 format is different to another?
Thanks.
You have to convert your bytes into an Uint8List object not a List<int>:
File _img = File(
'/data/user/0/com.example.test3/app_flutter/2020-10-29T17:18:56.210347.png');
final bytes = Uint8List.fromList(_img.readAsBytesSync());
final imgBase64 = base64Encode(bytes);
print(imgBase64);
I find that this case by the 'print' function cannot display fully base64 code.
If want to verify it, need to export to text file:
_write(String text) async {
final File file = File('/storage/emulated/0/xxx/my_file.txt');
await file.writeAsString(text);
print(file);
}

Encode and Decode base 64 in Node JS

I have a node JS program to concat a String to an encrypted message and base-64 encoding it. in my server program when I tried to base-64 decode, i am not getting the originally generated encrypted message.
i have replicated that problem in simple program below.
In this program,
I pass an encrypted and base 64 encoded message,
decode them, concatenating another string
encode the finalMessage
decode the finalMessage
Split them to get the encrypted message
encode the encrypted message to compare with original message.
the Result is - the original message passed to this function is not the same as the final message.
function decodeAndEncode(message) {
console.log("message---"+message)
const buffer = Buffer.from(message, 'base64');
console.log("buffer---"+buffer)
const updatedStringBuffer = Buffer.from('648f3ec157637553f170bccfe56bc32058d11741d016bf120e7001148b19a4d1');
const finalEncodedMsg = Buffer.from(updatedStringBuffer+"|"+buffer).toString('base64')
console.log("updatedMessage ---"+finalEncodedMsg);
const updatedMessageBuffer = Buffer.from(finalEncodedMsg, 'base64');
console.log("updatedMessageBuffer ---"+updatedMessageBuffer);
const getBackOriginalMsg = updatedMessageBuffer.toString('utf-8',updatedStringBuffer.length+1);
console.log("getBackOriginalMsg---"+getBackOriginalMsg);
const encodedMessageBack = Buffer.from(getBackOriginalMsg).toString('base64')
console.log("encodedMessageBack--- "+encodedMessageBack)
}
const message = 'KCof0N56Z0X5piDvPO4FRL6e80oOxxPzzTMie+QRUy00RzwBn1qubNTtt8z5J+LykqlbcWSWfjGarNr4c40I+RdrI+Fi1r/wCs2ql0kvYYapTaaz9lT2EeMuwTp//kyVDUxaaHmBGaN1Ai7DQz44yKAwAnStWFP/lAuxLReQFp4A8wg9e22irkvC3bIMgpKUIheo/58WD03roH5IQsfIsY7oveODIR5s+T1lmIYBBH0IXZqwDOQpArcy82RMMCme6unhJZPIsWqSlVAEWtD89muXdnpvQRFH88exZ1v3WiYYnlJruFoGz7Yi19nrvYI9gkhoee5Idi2m1w1LmDw8EQ==';
const enc = decodeAndEncode(message)

Converting cjson json to string

I am using cjson library to create json and send it to server. I am using following code
char *string = cJSON_Print(json);
For which I get following output
{
"type1": "car",
"type2": "bus",
}
But the server expects it in string format like this
"{\n\t\"type1\":\t\"car\",\n\t\"type2\":\t\"bus\"\n}"
I am confused how can I achieve such conversion?

WebApi impossible to send proper encoded file to front (angularjs)

I am receiving a Base64 encoded string that contains a word document (generated by thunderHead), that I decode with:
byte[] dataBytes = Convert.FromBase64String(data)
When I write directly this byteArray into a file:
File.WriteAllBytes("myFile.doc", dataByte);
The word document is properly written (é, è are shown properly), but it's not the case when I send this byteArray to the front (angularjs):
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(dataByte);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msword");(also tried "application/octet-stream")
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachement");
response.Content.Headers.ContentDisposition.Filename = "toto.doc"
return response;
I can't figure it out....some help would be appreciated thanks!
Solved my problem, as #peco pointed, the problem was on the front part, I was doin a $http.post(url, data).then ... and missed to add {responseType: 'arrayBuffer'} as a third parameter: $http.post(url, data, {responseType: 'arrayBuffer'}).then... Thanks.

Unmarshal JSON object of strings, ints and arrays into a map

I like to unmarshal a JSON string using Decode():
var message Message
decoder := json.NewDecoder(s)
err = decoder.Decode(&message)
My data structure is
type Message map[string]interface{}
The test data is as follows:
{
"names": [
"HINDERNIS",
"TROCKNET",
"UMGEBENDEN"
],
"id":1189,
"command":"checkNames"
}
It's working fine for numbers and strings, but with the string array I get following panic:
panic: interface conversion: interface is []interface {}, not []string
this is not possible by conversion because a slice of struct != slice of interface it implements!
either you can get the elements one by one and put them into a []string like this: http://play.golang.org/p/1yqScF9yVX
or better, use the capabilities of the json package to unpack the data in your model format : http://golang.org/pkg/encoding/json/#example_Unmarshal

Resources