Vala - Bytes convert to string? - string

I have a GLib.Bytes object.
I want to print it and use it as a string like this:
Bytes bytes = new Bytes({65, 66, 67});
print(bytes); // <-- ERROR
How can I convert it to string?

Get raw byte array uint8[] with Bytes.get_data() and cast it as string.
Example:
Bytes bytes = new Bytes({65, 66, 67});
string str = (string)bytes.get_data();
print(str);
Output:
ABC

Related

Hashlib.md5 a varaible bytes object with prefix b''

How i should pass the value of string x (\0x02...) to the hashlib.md5 function and get the same output as result0 (05db8f903dc9adc5874eb2cab3aa725b) ?
Note:
b'' is a prefix, that causes the following string to be interpreted as a bytes-type object. The bytes function takes a string and returns a bytes object.
This is my code:
import hashlib
result0 = hashlib.md5(b'\x02\x6d\x79\x5f\x70\x61\x73\x73\xe2\x5b\xc4\x1c\x40\x8a\x54\x84\x3e\xbd\xee\xab\xdc\x69\x90\xc0')
print(result0.hexdigest())
x= '\x02\x6d\x79\x5f\x70\x61\x73\x73\xe2\x5b\xc4\x1c\x40\x8a\x54\x84\x3e\xbd\xee\xab\xdc\x69\x90\xc0'
yourstring = x.encode('ascii', 'ignore')
var = bytes(yourstring)
result1 = hashlib.md5(var)
print(result1.hexdigest())

CAPL convert an Byte array to ascii string

i have an byte array that i need to convert to ascii string before i save it as a system variable.
so i have a byte array like [63 6c 65 61 72]. i need to convert it to 'clear' before i store it in a system variable.
for (i = 0; i < elcount(gByteValue); i++)
{
snprintf(tmp, elcount(tmp), "%.2X ", gByteValue[i]); // byte to HEX convert
strncat(gStringValue, tmp, elcount(gStringValue)); // Concatenate HEX value to output string
}
write("String : %s", gStringValue);

Python 3.8: Bytes to a list of hex

I'm currently banging by head bloody over this problem I have:
I have a method where I receive a byte input of a defined length of 8:
def ConvertToHexAndSend(serialnumber):
if len(serialnumber) != 8:
throw a good exception...
Some good converter code here..
The method shall then take the serialnumber and split it into a list with the size 4.
Example:
serialnumber = b'12345678'
Expected output:
[0x12, 0x34, 0x56, 0x78]
What I have tried so far:
new_list = list(serialnumber) # Gives a list of [49, 50, 51, 52, 53, 54, 55, 56] and then:
first byte = new_list[0] << 8 + new_list[1] # gives some riddish value
and
hex(serialnumber[0]) # Gives '0x32' string
and
first_byte = serialnumber[0] + serialnumber[1] # Gives int 99
But no success so far.. any idea?
You want something like:
result = list(bytes.fromhex(serialnumber.decode('ascii')))
Your bytes are represent a hex-string. So use the bytes.fromhex to read that (converting the bytes to str first), and you can use list to get a list of int objects from the new resulting bytes object

How do I convert a byte array with null terminating character to a String in Kotlin?

When I attempt to retrieve a value from the device via Bluetooth, it comes out in ASCII, as a null terminated big-endian value. The device software is written in C. I want to retrieve the decimal value, i.e. 0 instead of 48, 1 instead of 49, 9 instead of 57, etc.
#Throws(IOException::class)
fun receiveData(socket: BluetoothSocket ? ): Int {
val buffer = ByteArray(4)
val input = ByteArrayInputStream(buffer)
val inputStream = socket!!.inputStream
inputStream.read(buffer)
println("Value: " + input.read().toString()) // Value is 48 instead of 0, for example.
return input.read()
}
How can I retrieve the value I want?
It's easy to do with bufferedReader:
val buffer = ByteArray(4) { index -> (index + 48).toByte() } // 1
val input = ByteArrayInputStream(buffer)
println(input.bufferedReader().use { it.readText() }) // 2
// println(input.bufferedReader().use(BufferedReader::readText)) // 3
Will output "0123".
1. Just stubs the content of a socket using an init function that sets first element of the buffer to 48, second to 49, third to 50 and fourth to 51.
2. The default charset is UTF-8, that is a "superset" of ASCII.
3. It's just another style of calling { it.readText() }.
My function ultimately took the following form. This allowed me to retrieve all 5 digits in decimal form:
#Throws(IOException::class)
fun receiveData(socket: BluetoothSocket ? ): String {
val buffer = ByteArray(5)
(socket!!.inputStream).read(buffer)
println("Value: " + String(buffer))
return String(buffer)
}
For my particular problem, the input variable was being created before reading the data into the buffer. Since the read method is called for every index in the data, I was only getting the first digit.
See the Java method public int read() for an explanation:
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

Node - Convert from base64 string to utf-8 byte code array

I'm currently trying to convert images from a base 64 string to a utf-8 byte code array. Below is the code I'm attempting to use with the base 64 string replaced for clarity. I'm getting what looks to be a utf-8 byte array, but have not been able to find a way to verify. Any help with a resource to help verify or if the code is incorrect?
const b64 = '...base64 string';
// convert to utf8 string
const utf8 = (Buffer.from(b64, 'base64')).toString('utf8');
// create a buffer of utf8 string
const buff = Buffer.from(utf8, 'utf8');
//
const arr = [...buff];

Resources