Convert a mixed string to UTF16 with PHP [closed] - unicode

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a string in PHP like this:
INPUT = "Γιώργο αν στείλεις αυτό ακριβώς (:
Its a mixed string with GREEK and LATIN characters at the same time. I want all characters in a given string to be converted to this string
OUTPUT = 0022039303B903CE03C103B303BF002003B103BD002003C303C403B503AF03BB03B503B903C2002003B103C503C403CC002003B103BA03C103B903B203CE03C200200028003A
How can I convert the input to this kind of output?

It took me a while to figure out but it seems that what you want to do is to convert a UTF-8 string to an uppercase binary dump of its UTF-16 representation. It's as simple as this:
$input = '"Γιώργο αν στείλεις αυτό ακριβώς (:';
$output = mb_strtoupper(bin2hex(mb_convert_encoding($input, 'UTF-16BE', 'UTF-8')));
The BE suffix indicates Big Endian, which seems to be the expected byte order according to your example.

Have you tried iconv? Maybe something like this:
(EDIT) i understand your main goal is to be able this converted string to be transported via http to your service, in that case you can use base64_encode() on the result.
see the complete code example:
<?php
$string = str_replace(' ', '', "Γιώργο αν στείλεις αυτό ακριβώς");
$converted = iconv('UTF-8', 'UTF-16', $string);
$encoded = base64_encode($converted);
var_dump($encoded);
var_dump(iconv('UTF-16', 'UTF-8', base64_decode($encoded)));
?>
You will get:
encoded: string '/v8DkwO5A84DwQOzA78DsQO9A8MDxAO1A68DuwO1A7kDwgOxA8UDxAPMA7EDugPBA7kDsgPOA8I=' (length=76)
decoded: string 'Γιώργοανστείλειςαυτόακριβώς' (length=54)

Related

Need help understanding perl code [duplicate]

This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.

Count the number of words for each contributor [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I count the number of words that each contributor wrote in a repository ?
It is best solved on a local clone of that GitHub, in order to analyze the git log and check each contributor.
See "getting contributor stats", based on:
git log --author="Jared Barboza" --pretty=tformat: --numstat | \
grep -v public/javascripts/jquery | \
gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END \
{ printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }' -
But that is for lines added/removed for a given contributor.
(a bit like in "Git: How to estimate a contribution of a person to my project in terms of added/changed lines of code?")
You can combined a similar approach with the one described in "Quantifying the amount of change in a git diff?".
Or you can use a dedicated program for that, like git-wordcount.

add an option to the perl script command line [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
assume this is file.txt
Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final
Annette Adams :8:24:19:24:10:12:43:35:98
Mary Beard:9:30:19:23:10:14:29:39:87
Antoinette Brown:9:16:18:22:9:12:19:31:79
.
.
.
is it possible in Perl to write a script with some options which user can pass to command line and get the result.
for example getAverage.pl -l 3 file.txt prints the average of Lab3 Marks.
if it is possible what is the overall process?
Try http://perldoc.perl.org/Getopt/Long.html From the documentation:
The Getopt::Long module implements an extended getopt function called
GetOptions(). This function adheres to the POSIX syntax for command
line options, with GNU extensions. In general, this means that options
have long names instead of single letters, and are introduced with a
double dash "--". Support for bundling of command line options, as was
the case with the more traditional single-letter approach, is provided
but not enabled by default.
For general information about accessing command line options in Perl, read the perlvar documentation: http://perldoc.perl.org/perlvar.html#$ARGV
I put together a quick and dirty solution in python. Should be easy to port it to perl.
import sys
lines = open("file.txt",'r').readlines()
if len(sys.argv) > 2 and sys.argv[1] == "-l":
try:
number = int(sys.argv[2])
except:
print "Input a number after the -l switch."
average = 0
for i in lines:
try:
average += int(i.split(":")[number])
except:
pass
average /= 3
print "The average score for %s is %d" \
%(lines[0].split(":")[number], average)
else:
print "Computer says no."

BIGINT scalar function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to??
Create a scalar function that returns a bigint.
The function takes 2 inputs of bigint.
The function multiplies the inputs and returns the result.
Thank You,
How about something like this:
CREATE FUNCTION [dbo].[testbigint]
(
#int1 bigint,
#int2 bigint
)
RETURNS bigint
AS
BEGIN
-- Declare the return variable here
DECLARE #returnVal bigint
set #returnVal = #int1* #int2
-- Return the result of the function
RETURN #returnVal
END

How to retrieve the values of required keys in Perl [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a hash with keys and values. How can I retrieve the values of the desired keys?
%a = qw(genea brain geneb heart genec kidney gened eye);
Now I want to retrieve the value for the keys genec and gened. How can I do this?
To get a list of the values for many keys at once, use a hash slice:
#lots_of_values = #hash{ #lots_of_keys };
Because a list is the result, you use the # sigil even though it is a hash; the values will be the order of the keys specified, including undef values where the specified keys don't exist in the hash.
It sounds like all you're asking is how to access elements of a hash. As Quentin indicates, this is trivially google-able.
The perldata doc covers basic questions, and perlfaq4 covers many other hash questions.
That said, to answer your question:
print $a{'genec'};
print $a{'gened'};
I also would not declare your hash in that way, as it's unclear what is a key and what is a value. Instead, consider:
my %a = ('genea' => 'brain', 'geneb' => 'heart'); # etc.
$GENEC = $a{genec};
$GENED = $a{gened};
Please get yourself a copy of Learning Perl. You'll be glad you did.