Golang string end character - go

I'm a newbie in Golang, so I'm playing with some algorithms and i have a little problem.
In java for insert an end string in char array I can do like this:
String str = "Mr John Smith ";
char[] arr = str.toCharArray();
arr[12] = '\0';
But in Golang I'm trying like this:
str := []byte("Mr John Smith ")
str[12] = '\0'
But this code didn't work

That's not a valid syntax for a rune literal with a 0 value. You can use the hex escape sequence
str[12] = '\x00'
If you really need an octal value, it requires 3 digits
str[12] = '\000'
Or just assign a literal 0
str[12] = 0
You can see the valid rune literal escape sequences in the specification: https://golang.org/ref/spec#Rune_literals

Related

Replace decimal numbers in string by hex equivalent

I have a huge set of strings like:
// Register 10:
typedef struct RegAddr_10
{
uint8 something: 4;
uint8 something_else: 4;
} tRegAddr_10;
and want to convert all register addresses (given in decimal numbers) to hexadecimal. Other numbers can occur within each typedef; therefore I have to consider the Reg part as a kind of delimiter. The example should result in:
// Register 0x0A:
typedef struct RegAddr_0x0A
{
uint8 something: 4;
uint8 something_else: 4;
} tRegAddr_0x0A;
My solution is this:
class String
def convert_base(from, to)
self.to_i(from).to_s(to)
end
end
new = text.gsub(/Reg\D*\d*/) do |number|
number.gsub(/(\d+)/) {'0x'+$1.convert_base(10,16)}
end
It works, but:
(How) is it possible to do this with one gsub only?
How can I make the conversion generate 2-digit-hex numbers in upper case, e.g. 10 → 0x0A, not 0xa?
Code
R = /
(?: # begin non-capture group
^ # match beginning of line
\/{2}\s+Register\s+ # match string
| # or
\s+RegAddr_ # match string
| # or
\s+tRegAddr_ # match string
) # close non-capture group
\K # discard everything matched so far
\d+ # match >= 1 digits
/x # extended mode
def replace_with_hex(str)
str.gsub(R) { |s| "0x%02X" % s }
end
The part of the format string for String#% that follows the percent character is: 0, meaning pad left with zeros, 2 for field width and X to convert to hex with letters A-F in capitals (x for lower case).
Example
str = <<_
// Register 10:
typedef struct RegAddr_10
{
uint8 something: 4;
uint8 something_else: 4;
} tRegAddr_10;
_
puts replace_with_hex(str)
prints:
// Register 0x0A:
typedef struct RegAddr_0x0A
{
uint8 something: 4;
uint8 something_else: 4;
} tRegAddr_0x0A;
Alternatives
If you are less fussy:
R = /
[\s|t] # match whitespace or t
Reg\D+ # match string
\K # discard everything matched so far
\d+ # match >= 1 digits
/x # extended mode
works as well.
You could also change the operative line of replace_with_hex to:
str.gsub(R, "0x%02X" % $~[0])

Processing: replace space with non-breaking space

I keep getting an error of "Badly formed character constant" when using this code in Processing 1.5.1. I want to replace all spaces with non-breaking spaces in my String. Any help would be appreciated.
String newStr;
String S = "Hello there world"
char c = '\u00A0';
newStr = S.replace(' ', c);
Processing indicates unicode without quotes and with the prefix 0xTry this:
String newStr;
String S = "Hello there world"
char c = 0x00A0;
newStr = S.replace(' ', c);

NSMutableString stringWithFormat

I have java code that prepares a message for a MD5 munge
private static char[] jimsCopyRight = {
'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h',
't', ':', ' ', 0xa9, ' '};
which is used in
StringBuffer message = new StringBuffer();
message.append(name.toLowerCase());
message.append(new String(jimsCopyRight));
When I print out the message using
for(int i = 0; i < message.length(); i++){
System.out.println(" i = " + i + " char " + message.substring(i, i + 1) + " charAT " + message.charAt(i));
}
I get i = 14 char \251 charAT \251 and the message.toString is jimCopyright: \251
I need to construct a NSMutableString with the same characters.
Among the things I have tried
wDevCopyright = [NSString stringWithFormat:#"jimCopyright: %c ", 0xa9];
for(int i = 0; i < [message length]; i++){
NSLog(#"i = %d char %c %d", i, [message characterAtIndex:i], [message characterAtIndex:i]);
}
Which gives me i = 14 char © 169
Any help in getting the NSMutableString to be the same as the StringBuffer will be appreciated.
The problem is that when I munge the two strings in MD5 I get different results when I add the 0xa9. The prints are just for getting a look at the strings.
I'm thinking it has something to do with the char[] in Java and the construct of the NSMutableString. I do not believe they are the same values.
I have some C code and it declares the copyright as
#define jimsCopyRight "Copyright: � "
The Java MD5 and C MD5 of the copyright are the same.
The copyright symbol is what you wanted, right? According to your question, that's what you got. So what's the problem?
(This is why magic numbers are bad. I can't tell from your code alone what encoding's 0xa9 you wanted.)
If you're wondering why it says 169 instead of 251, it's because Java prints out the octal (base-8) character escape sequence, whereas %d in the C standard library, Core Foundation, and Foundation prints out the value as decimal (base-10). \251 is not the two hundred and fifty-first character. Use %o to print the value as octal, or %x to print it as hexadecimal (base-16). Or, use Calculator.app's Programming mode to convert 251 from octal to decimal.
BTW, you can use the %# format sequence and the NSUserName function to insert your username into the string, just like you did in the Java code. No need to hard-code it.
You’re missing an “s” in “jim[s]Copyright” in the Objective-C version.

Converting Decimal to ASCII Character

I am trying to convert an decimal number to it's character equivalent. For example:
int j = 65 // The character equivalent would be 'A'.
Sorry, forgot to specify the language. I thought I did. I am using the Cocoa/Object-C. It is really frustrating. I have tried the following but it is still not converting correctly.
char_num1 = [working_text characterAtIndex:i]; // value = 65
char_num2 = [working_text characterAtIndex:i+1]; // value = 75
char_num3 = char_num1 + char_num2; // value = 140
char_str1 = [NSString stringWithFormat:#"%c",char_num3]; // mapped value = 229
char_str2 = [char_str2 stringByAppendingString:char_str1];
When char_num1 and char_num2 are added, I get the new ascii decimal value. However, when I try to convert the new decimal value to a character, I do not get the character that is mapped to char_num3.
Convert a character to a number in C:
int j = 'A';
Convert a number to a character in C:
char ch = 65;
Convert a character to a number in python:
j = ord('A')
Convert a number to a character in Python:
ch = chr(65)
Most languages have a 'char' function, so it would be Char(j)
I'm not sure what language you're asking about. In Java, this works:
int a = 'a';
It's quite often done with "chr" or "char", but some indication of the language / platform would be useful :-)
string k = Chr(j);

how do you parse a string in vb6?

Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.
Given a string:
Dim mystring As String = "1234567890"
How do you loop in VB6 through each character and do something like
for each character in mystring
debug.print character
next
In C# i would do something like
char[] myChars = mystring.ToCharArray();
foreach (char c in theChars)
{
//do something with c
}
Any ideas?
Thanks a lot
You can use the 'Mid' function to get at the individual characters:
Dim i As Integer
For i = 1 To Len(mystring)
Print Mid$(mystring, i, 1)
Next
Note this is untested.
There is no possibility to use foreach on strings.
Use
Dim i As Integer
For i = 1 To Len(YourString)
Result = Mid$(YourString, i, 1)
Next
note that the type of Result is a length-1 string, no char or byte type.
If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.
Dim i As Long
For i = 0 To UBound(Data)
Result = Data(i) ' Type is Byte '
Next
This is much more efficient.
The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).
Dim str As String
Dim bytArray() As Byte
Dim count As Integer
str = "This is a string."
bytArray = str
For count = 0 To UBound(bytArray)
Debug.Print Chr(bytArray(count))
Next
Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.

Resources