I use Visual Basic to Simulate java program language Base64Url.decode, this is my code:
Java
import org.jose4j.base64url.Base64Url;
public class ss {
public static void main(String[] args) {
String Base64str = "q128qjafnAdcbkfrhfWim783bCvDWsak9bfe2ERjnAc";
byte[] decodeResult = Base64Url.decode(Base64str);
}
}
Visual Basic
Dim Base64str As String = "q128qjafnAdcbkfrhfWim783bCvDWsak9bfe2ERjnAc"
Dim decodeResult() As Byte = System.Convert.FromBase64String(Base64str)
It showed System.FormatException - Invalid length for a Base-64 char array.
How to solve it?
Related
I have a problem that I was going to work on arrays in WebAssembly and I wanted to use Java and C ++, and trying to do this, I ran into the following problems and I would like to ask for help:
Java: I'm using JWebAssembly
And we have a class that works on tables
import de.inetsoftware.jwebassembly.api.annotation.Export;
public class Add {
#Export
public static int[] add( int a[], int b ) {
for(int i = 0;i<b-1;i++){
a[i] += b ;
}
return a;
}
}
we convert it to wasm
import de.inetsoftware.jwebassembly.JWebAssembly;
import java.io.File;
import java.net.URL;
public class Wasm {
public static void main(String[] args) {
File wasmFile = new File("testTable.wasm");
JWebAssembly wasm = new JWebAssembly();
Class clazz = Add.class;
URL url = clazz.getResource(
'/' +
clazz.getName().replace('.', '/') +
".class");
wasm.addFile(url);
String txt = wasm.compileToText();
System.out.println(txt);
wasm.compileToBinary(wasmFile);
}
}
and such an error comes out
Exception in thread "main" de.inetsoftware.jwebassembly.WasmException: Unimplemented Java byte code operation: 42 at Add.java:11
https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions
And I don't understand why because in this guy's presentation https://www.youtube.com/watch?v=93z9SaLQVVw (40 min+) you can see that it works and compiles
Now C++
I use emscripten, I wanted to do a bubble sort but for the sake of simplicity an example showing the problem
#include <emscripten.h>
using namespace std;
EMSCRIPTEN_KEEPALIVE
int* mainFunction(int table[], int length)
{
int* outTable = new int[length];
for(int i = 0;i<length; i++){
outTable[i] = table[i];
}
return table;
}
By running it with this command test.cpp -s WASM=1 -o test.html
after compiling it, files appear to me from which I extract the appropriate data and in javascript I set and import everything
let wasmExports = null;
let asmLibraryArg = {
abort: () => {},
emscripten_resize_heap: () => {},
};
let info = {
env: asmLibraryArg,
wasi_snapshot_preview1: asmLibraryArg,
};
async function loadWasm() {
let response = await fetch("test.wasm");
let bytes = await response.arrayBuffer();
let wasmObj = await WebAssembly.instantiate(bytes, info);
wasmExports = wasmObj.instance.exports;
}
loadWasm();
and later in the code I use
console.log(wasmExports);
console.log(wasmExports._Z12mainFunctionPii(table, table.length));
results
and when I throw in some array of integers it only throws me the number 0 and I have no idea how to get out of it. Or maybe someone knows another language in which it is possible to compile for wasm and then run it on the website?
I am new to protocol buffers so I was trying out an example code. My proto file code is below:
syntax="proto2";
package test;
option java_package="com.example.test";
message Test1 {
required int32 a = 1;
}
I compiled it using protec correctly. After that I wanted to use it out in a Java code. The code is
import com.example.test.Test1OuterClass;
import com.example.test.Test1OuterClass.Test1;
import java.io.*;
import java.util.*;
public class Testing {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int a = sc.nextInt();
Test1.Builder t = Test1.newBuilder();
t.setA(a).build();
}
}
Now I want to implement encoding in this but I am not able to do it. I searched online and read the Google documentation but couldn't understand how to do it. Can someone tell me how to perform basic encoding here?
Useful links related to encoding in protobufs are appreciated too.
Test1 obj = t.setA(a).build();
then
byte[] arr = obj.toByteArray();
or
obj.writeTo(outputStream);
I'd like to encode a protopuf in python and send it via redis to a java application where I decode it.
Atm I can print the data in the java app and the values are correct. But everytime I receive data I get the following exception:
InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag
I tried it also with Jedis but the data was wrong there. Also tried to send it without the bytearray cast from python but I get the same error here.
Does anyone have an idea concerning this issue?
Code on python side:
tele_bytes = array("B")
// tele_bytes data comes from serial interface
tele_bytes[1] = ser.read()
tele_bytes[2] = ser.read()
raw_data = ''.join(chr(x) for x in [
tele_bytes[1],
tele_bytes[2]
])
gw_id = '12345678'
reading = Reading_raw_data_pb2.ReadingRaw()
reading.timestamp = int(time())
reading.gw_id = gw_id
reading.raw_data = raw_data
reading_string = reading.SerializeToString()
r_server.lpush("testID", bytearray(reading_string))
Code on Java Side:
import com.google.protobuf.InvalidProtocolBufferException;
import redis.clients.jedis.BinaryJedis;
import protos.Reading4Java;
import java.io.IOException;
import java.util.List;
public class SimpleSubByte {
public static Reading4Java.ReadingRaw trRaw = null;
public static void main(String[] args) {
BinaryJedis binaryJedis = new BinaryJedis("test.id.local");
while (true) {
List<byte[]> byteArray = binaryJedis.brpop(0, "testID".getBytes());
for (byte[] e : byteArray) {
// System.out.println(e);
try {
trRaw = Reading4Java.ReadingRaw.parseFrom(e);
} catch (InvalidProtocolBufferException e1) {
e1.printStackTrace();
}
System.out.println(trRaw);
}
}
}
}
Protobuf file python:
package ttm
message ReadingRaw {
required string gw_id = 1; // gateway id (e. g. mac address)
required bytes raw_data = 2; // raw data from serial interface
optional int64 timestamp = 3; // timestamp of data reading from sensor device
}
Protobuf File for java:
package protos;
option java_outer_classname = "TireReading4Java";
message TireReadingRaw {
required string gw_id = 1; // gateway id (e. g. mac address)
required bytes raw_data = 2;
optional int64 timestamp = 3;
}
I have a problem. I wrote this code that reads a string from a txt file and I exported with the first method a int while the second one particular string. This method is already running but I have used the apache library, now I wanted to rewrite it in Java standard libraries. I have tried this, but I have had problems. Could someone help me? Thank you very much.
package ausiliare;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.*;
public class Read {
public static int getInt() throws IOException {
String content = null;
File folder = new File("C:\\Solution.txt");
content = FileUtils.readFileToString(folder) + "\n";
int outside = Integer.parseInt(content.substring(0,
content.indexOf("[")).trim());
return outside;
}
public static String getString() throws IOException {
String content = null;
File folder = new File("C:\\Solution.txt");
content = FileUtils.readFileToString(folder) + "\n";
String remainingString = content.substring(content.indexOf(" ["),
content.lastIndexOf("]") + 1);
// System.out.println(remainingString);
return remainingString;
}
public static String[] arg() throws IOException {
String[] strArray = getString().split(" ");
// System.out.println(Arrays.toString(strArray));
return strArray;
}
}
Ps: The input file is txt (for example):
50 [8,24,-22] [-8,34,12] [19,14,47] [-49,32,44] [-41,16,-6] [-49,-11,43]
Where the first method extracts the int 50 and the second extraction method extracts the remaining
content = new String(Files.readAllBytes(folder.toPath()),
StandardCharsets.UTF_8);
The missing part is the knowledge of the Files class.
There is a List<String> readAllLines too.
The character set parameter is optional and defaults to the current operating system's encoding - not very portable to other computers.
I am receiving a String via an object from an axis webservice. Because I'm not getting the string I expected, I did a check by converting the string into bytes and I get C3A4C2 BDC2A0 C3A5C2 A5C2BD C3A5C2 90C297 in hexa, when I'm expecting E4BDA0 E5A5BD E59097 which is actually 你好吗 in UTF-8.
Any ideas what might be causing 你好吗 to become C3A4C2 BDC2A0 C3A5C2 A5C2BD C3A5C2 90C297? I did a Google search but all I got was a chinese website describing a problem that happens in python. Any insights will be great, thanks!
You have what is known as a double encoding.
You have the three character sequence "你好吗" which you correctly point out is encoded in UTF-8 as E4BDA0 E5A5BD E59097.
But now, start encoding each byte of THAT encoding in UTF-8. Start with E4. What is that codepoint in UTF-8? Try it! It's C3 A4!
You get the idea.... :-)
Here is a Java app which illustrates this:
public class DoubleEncoding {
public static void main(String[] args) throws Exception {
byte[] encoding1 = "你好吗".getBytes("UTF-8");
String string1 = new String(encoding1, "ISO8859-1");
for (byte b : encoding1) {
System.out.printf("%2x ", b);
}
System.out.println();
byte[] encoding2 = string1.getBytes("UTF-8");
for (byte b : encoding2) {
System.out.printf("%2x ", b);
}
System.out.println();
}
}
public class Encoder{
public static void main(String[] args) throws Exception {
String requestString="你好";
String ISO = new String(requestString.getBytes("gb2312"), "ISO8859-1");
String plaintxt = new String(ISO.getBytes("ISO8859-1"), "gb2312");
plaintxt.getBytes("UTF-8");
}
}