How to convert Base58check to HexString in Dart language?
Like on this site: https://www.btcschools.net/tron/tron_tool_base58check_hex.php
Take this data as an example:
Base58 String: TNNukJf11S9iJr1RmgViE2kr8LdFCBBeVb - this TRON Wallet
Hex String: 41881d16d9cd320c94d93acbbe52128ccd0ff9a7d4
Related
I wanted to replace the first character of a String and got it to work like this:
s.replaceSubrange(Range(NSMakeRange(0,1),in:s)!, with:".")
I wonder if there is a simpler method to achieve the same result?
[edit]
Get nth character of a string in Swift programming language doesn't provide a mutable substring. And it requires writing a String extension, which isn't really helping when trying to shorten code.
To replace the first character, you can do use String concatenation with dropFirst():
var s = "😃hello world!"
s = "." + s.dropFirst()
print(s)
Result:
.hello world!
Note: This will not crash if the String is empty; it will just create a String with the replacement character.
Strings work very differently in Swift than many other languages. In Swift, a character is not a single byte but instead a single visual element. This is very important when working with multibyte characters like emoji (see: Why are emoji characters like 👩👩👧👦 treated so strangely in Swift strings?)
If you really do want to set a single random byte of your string to an arbitrary value as you expanded on in the comments of your question, you'll need to drop out of the string abstraction and work with your data as a buffer. This is sort of gross in Swift thanks to various safety features but it's doable:
var input = "Hello, world!"
//access the byte buffer
var utf8Buffer = input.utf8CString
//replace the first byte with whatever random data we want
utf8Buffer[0] = 46 //ascii encoding of '.'
//now convert back to a Swift string
var output:String! = nil //buffer for holding our new target
utf8Buffer.withUnsafeBufferPointer { (ptr) in
//Load the byte buffer into a Swift string
output = String.init(cString: ptr.baseAddress!)
}
print(output!) //.ello, world!
I am looking to see if it is possible to get a string that has a variable in it from a plist.
So just like you would do normally.
let string1 = "a string variable"
print("this is \(string1)"
I wanted to just put in my plist file "this is \(string1)"
So how can you format it in a plist file or can you format it when you pull it out of the plist file into a String.
Or do I just need to rebuild the whole string after I pull it out of the plist file?
Thanks
The proper solution would be to use string formatting, not string interpolation.
Put the following value in the plist:
this is %#
Then load that value as needed. Use it with String(format:...).
let format = ... // the format string loaded from the plist
let string1 = "a string variable"
let result = String(format: format, string1)
If format is this is %# then the result will be:
this is a string variable
Use %d, for Int variables. Use %f for Double. See the documentation on String Format Specifiers for more details on the format specifiers.
I'm receiving via a REST API a string which contains unicode encoded characters in form of \uXXXX
e.g. Ain\u2019t which should be Ain’t
Is there a nice way to convert these?
You can use \u{my_unicode}:
print("Ain\u{2019}t this a beautiful day")
/* Prints "Ain’t this a beautiful day"
From the Language Guide - Strings and Characters - Unicode:
String literals can include the following special characters:
...
An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit
hexadecimal number with a value equal to a valid Unicode code point
You can apply a string transform StringTransform:
extension String {
var decodingUnicodeCharacters: String { applyingTransform(.init("Hex-Any"), reverse: false) ?? "" }
}
let string = #"Ain\u2019t"#
print(string.decodingUnicodeCharacters) // "Ain’t\n"
So I know how to convert String to utf8 format like this
for character in strings.utf8 {
// for example A will converted to 65
var utf8Value = character
}
I already read the guide but can't find how to convert Unicode code point that represented by integer to String. For example: converting 65 to A. I already tried to use the "\u"+utf8Value but it still failed.
Is there any way to do this?
If you look at the enum definition for Character you can see the following initializer:
init(_ scalar: UnicodeScalar)
If we then look at the struct UnicodeScalar, we see this initializer:
init(_ v: UInt32)
We can put them together, and we get a whole character
Character(UnicodeScalar(65))
and if we want it in a string, it's just another initializer away...
1> String(Character(UnicodeScalar(65)))
$R1: String = "A"
Or (although I can't figure out why this one works) you can do
String(UnicodeScalar(65))
http://play.golang.org/p/SKtaPFtnKO
func md(str string) []byte {
h := md5.New()
io.WriteString(h, str)
fmt.Printf("%x", h.Sum(nil))
// base 16, with lower-case letters for a-f
return h.Sum(nil)
}
All I need is Hash-key string that is converted from an input string. I was able to get it in bytes format usting h.Sum(nil) and able to print out the Hash-key in %x format. But I want to return the %x format from this function so that I can use it to convert email address to Hash-key and use it to access Gravatar.com.
How do I get %x format Hash-key using md5 function in Go?
Thanks,
If I understood correctly you want to return the %x format:
you can import "encoding/hex" and use the EncodeToString method
str := hex.EncodeToString(h.Sum(nil))
or just Sprintf the value:
func md(str string) string {
h := md5.New()
io.WriteString(h, str)
return fmt.Sprintf("%x", h.Sum(nil))
}
note that Sprintf is slower because it needs to parse the format string and then reflect based on the type found
http://play.golang.org/p/vsFariAvKo
You should avoid using the fmt package for this. The fmt package uses reflection, and it is expensive for anything other than debugging. You know what you have, and what you want to convert to, so you should be using the proper conversion package.
For converting from binary to hex, and back, use the encoding/hex package.
To Hex string:
str := hex.EncodeToString(h.Sum(nil))
From Hex string:
b, err := hex.DecodeString(str)
There are also Encode / Decode functions for []byte.
When you need to convert to / from a decimal use the strconv package.
From int to string:
str := strconv.Itoa(100)
From string to int:
num, err := strconv.Atoi(str)
There are several other functions in this package that do other conversions (base, etc.).
So unless you're debugging or formatting an error message, use the proper conversions. Please.