Converting a string to bytes - string

im fairly new to OCaml, so excuse any stupid mistake.
Im trying to modify the elements of a string, yet it wont allow me, saying it is expecting an expression of type bytes. After doing some reading, I know why, so I tried to convert my string to bytes, with no success. I've looked everywhere on how to convert a string to bytes, but can't seem to find anything. Can someone help me?

There is a function Bytes.of_string that does this.
Bytes.of_string "my string abc";;
- : bytes = Bytes.of_string "my string abc"
It's interesting to note that the toplevel expression printer (the P part of REPL) prints byte values using a Bytes.of_string call.
You can convert back to a string with Bytes.to_string.
# let b = Bytes.of_string "hal9000";;
val b : bytes = Bytes.of_string "hal9000"
# Bytes.to_string (Bytes.map (fun c -> Char.chr (Char.code c + 1)) b);;
- : string = "ibm:111"

Related

How to know if there one or more Cyrillic symbol in String and count them (Kotlin)

I'm working on QR-parser. In my QR I have a field "encoding", which one shows us a string encoding ("1" = w1251, "2" = UTF8, "3" = KOI8-R). I need decode 1, 3 in UTF-8, so it's ok:
private fun checkEncoding(encoding: String, decodedString: String) =
when (encoding) {
"1" -> decodedString.toByteArray(Charset.forName("windows-1251")).toString(Charset.forName("UTF-8"))
"3" -> decodedString.toByteArray(Charset.forName("KOI8-R")).toString(Charset.forName("UTF-8"))
"2" -> decodedString
else -> throw ErrorsBuilder.UNKNOWN_ENCODING_ERROR.unknownEncodingException(decodedString)
}
But sometimes we get wrong QRs, which has encoding = 1, but string in UTF8.
We'd like to work with this situation, maybe anyone can help. We decided to make next logic: if string has Russian symbols -> do nothing with it, it's UTF8. else -> do checkEncoding()
First idea: check if they're Russian letters in string. I did this:
fun main() {
val string1 = "Name=Филиал"
val string2 = "Name=СчеС"
val string3 = "ФФФ"
println(parse2(string1)) //false
println(parse2(string2)) //false
println(parse2(string3)) //true }
fun parse2(string: String): Boolean {
return string.matches("[а-яёА-ЯЁ]+".toRegex())
}
How can u check that string has at least one Russian symbol with many English? Something like matchesAny()?
The second problem that in String "ST00011|Name=СчеС" symbols "Р", "С" are Russian too. I decided to make some counter, which count Russian symbols from string and compare that number with string length.
But I don't know how to perform it.
So maybe u have any Ideas and better solutions for my situation? Or can give me the answer on question from Title?
Thanks a lot
What about a simple regex check:
Regex("[\\u0400-\\u04FF]").containsMatchIn(inputString)

How to convert a variable to a raw string?

If I have a string, "foo; \n", I can turn this into a raw string with r"foo; \n". If I have a variable x = "foo; \n", how do I convert x into a raw string? I tried y = rf"{x}" but this did not work.
Motivation:
I have a python string variable, res. I compute res as
big_string = """foo; ${bar}"""
from string import Template
t = Template(big_string)
res = t.substitute(bar="baz")
As such, res is a variable. I'd like to convert this variable into a raw string. The reason is I am going to POST it as JSON, but I am getting json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 620 (char 619). Previously, when testing I fixed this by converting my string to a raw string with: x = r"""foo; baz""" (keeping in line with the example above). Now I am not dealing with a big raw string. I am dealing with a variable that is a JSON representation of a string where I have replaced a single variable, bar above, with a list for a query, and now I want to convert this string into a raw string (e.g. r"foo; baz", yes I realize this is not valid JSON).
Update: As per this question I need a raw string. The question and answer flagged in the comments as duplicate do not work (res.encode('unicode_escape')).

How to convert string representing n bits to a byte string?

I'm pretty new to Python (never coded before in this language) and I need to pass a byte string to a function (as specified here[1]). It receives a string represents a binary number and I want to generate a byte string from it. I've tried countless things but I'm really stuck on how to do it, can you help me please?
I'm trying to pass this value to a library that handles DES, so I don't have any other option than doing it this way.
[1] https://www.dlitz.net/software/pycrypto/api/current/Crypto.Cipher.DES-module.html#new
from Crypto.Cipher import DES
key = "1101101110001000010000000000000100101000010000011000110100010100"
param = key.tobytes() # <-- The function I need
cipher = DES.new(key, DES.MODE_ECB)
Your key is in its current form a binary Number.
You could get the bytes (still as a string) from that simply with:
length = 8
input_l = [key[i:i+length] for i in range(0,len(key),length)]
And then convert each of these in a list to ints:
input_b = [int(b,base=2) for b in input_l]
The bytearray is then simply given by:
bytearray(input_b)
or
bytes(input_b)
depending on your usecase.
Converting this to a function is left as an exercise to the reader.

OCaml splitting bytes vs characters

It seems I'm a little confused on the best way to handle this. At the moment I am using Jane Street's Core. Split has a signature that looks like
split : t -> on:char -> t list
Which is fine. However, I noticed that any string I type is a "Bytes" type. This creates a problem when I try to split because it expects a char. My question is, what is the best way to handle this? Right now I'm doing
String.split "My string is here" (String.get " " 0)
But this seems very hacky. I feel like there should be a better solution to this. Can anyone help me? Thank you!
You can make a char using 's.
# "x";;
- : string = "x"
# 'x';;
- : char = x
# String.split "My string is here" ' ';;
- : string list = ["My"; "string"; "is"; "here"]
It's possible to use regexp to solve your problem :
let () =
let l = Str.split (Str.regexp_string " ") "My string is here" in
List.iter (Printf.printf "%s\n") l

String manipulation in Lua: Swap characters in a string

I'm trying to do a function in Lua that swaps the characters in a string.
Can somebody help me ?
Here is an example:
Input = "This LIBRARY should work with any string!"
Result = "htsil biaryrs ohlu dowkrw ti hna ytsirgn!"
Note: The space is also swapped
Thank You Very Much :)
The simplest and clearest solution is this:
Result = Input:gsub("(.)(.)","%2%1")
This should do it:
input = "This LIBRARY should work with any string!"
function swapAlternateChars(str)
local t={}
-- Iterate through the string two at a time
for i=1,#str,2 do
first = str:sub(i,i)
second = str:sub(i+1,i+1)
t[i] = second
t[i+1] = first
end
return table.concat(t)
end
print(input)
print(swapAlternateChars(input))
Prints:
This LIBRARY should work with any string!
hTsiL BIARYRs ohlu dowkrw ti hna ytsirgn!
If you need the output as lower case you could always end it with:
output = swapAlternateChars(input)
print(string.lower(output))
Note, in this example, I'm not actually editing the string itself, since strings in Lua are immutable. Here's a read: Modifying a character in a string in Lua
I've used a table to avoid overhead from concatenating to a string because each concatenation may allocate a new string in memory.

Resources