473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

convert a string into another string

Hello!

I'd like to convert a string like this in perl:

example :
original string = 333445566
result string = 0x33,0x44,0x55,0x66
Mar 28 '13 #1
6 1899
Rabbit
12,516 Expert Mod 8TB
I don't know perl so someone else will have to help you with the code but what happened to the third "3"? Why did it get dropped?
Mar 28 '13 #2
Explain clear about your requirement
Mar 29 '13 #3
I've made a mistake.The 3rd "3" is only a typo.
original string = 33445566
result string = 0x33,0x44,0x55,0x66

So I need a script in perl which converts a string (with arbitrary length)into a string what I showed in result string.
This has to do the next :

1.create an array
2.insert "0x" before each element of the array and insert ","after each element of the array
Mar 29 '13 #4
Rabbit
12,516 Expert Mod 8TB
What do you have so far?
Mar 29 '13 #5
peoff
1
Hi,
Maybe that is what you are looking for:

Expand|Select|Wrap|Line Numbers
  1. # data
  2. $original_string = "33445566";
  3.  
  4. # test
  5. my $result_string = my_convert($original_string);
  6. print $result_string;
  7.  
  8. # the function for string modification
  9. sub my_convert(){
  10.     my $var = shift;
  11.     my $new_str = "";
  12.     foreach ( $var =~ /.{2}/sg){
  13.         $new_str .= "0x$_,";
  14.     }
  15.     chop($new_str);
  16.     return $new_str;
  17. }
notes:
1. Source string should be divisible by 2, so if you have string something like this "11223" => result will be "0x11,0x22". As you see "3" is skipped.
2. In your comments you talk about an array, so if you want to work with the array, you can modify code inside "foreach".
Mar 29 '13 #6
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. $, = ',';
  2. my $str = '3344556';
  3. my @parts;
  4. push @parts, "0x$1" while $str =~ /(..?)/g;
  5.  
  6. print @parts;
Mar 30 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: googlegroups | last post by:
Hi, I wonder if anyone can help? How do I see if one string is found within another string? or how many instances of one string, is found within another. ie. a="Sheree smells of poo"...
1
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use...
2
by: UJ | last post by:
I have need to convert a text string (with formatting) to an image. This is so that I can resize the image to different sizes and the formatting stays exactly the same regardless of the size. I...
5
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
3
by: bussiere maillist | last post by:
i've got a very long string and i wanted to convert it in binary like string = """Monty Python, or The Pythons, is the collective name of the creators of Monty Python's Flying Circus, a British...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
4
by: kvicky | last post by:
I have multiline textbox and I am trying to do a search functionality based on the input entered in this Textbox. I am able to convert into a string array if the search components are separated by...
8
by: =?Utf-8?B?UmljYXJkbyBRdWludGFuaWxsYQ==?= | last post by:
i need to convert data from string to nibble wich (nibble is a four bits representation) As example i have the following code string data1 = "12345678"; so ¿how can i convert this data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.