473,396 Members | 2,036 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,396 software developers and data experts.

XSLT binary to decimal

Dear ALL,

I have to convert a string of "10101010111"to a decimal number.

What is the standard solution in XSLT2.0 ?

Thanks a lot

RolfK
Nov 5 '08 #1
3 4941
RolfK wrote:
I have to convert a string of "10101010111"to a decimal number.

What is the standard solution in XSLT2.0 ?
http://www.dpawson.co.uk/xsl/rev2/fu...tml#d16818e694 has an
example of hex to decimal conversion. I applied that to binary to
decimal conversion as follows:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/2008/mf"
exclude-result-prefixes="xsd mf"
version="2.0">

<xsl:function name="mf:bin2dec" as="xsd:integer">
<xsl:param name="input" as="xsd:string"/>
<xsl:sequence
select="if (string-length($input) eq 1)
then xsd:integer($input)
else 2 * mf:bin2dec(substring($input, 1,
string-length($input) - 1)) + xsd:integer(substring($input,
string-length($input)))"/>
</xsl:function>

<xsl:template match="/">
<xsl:value-of select="mf:bin2dec('10101010111')"/>
</xsl:template>

</xsl:stylesheet>

but I have not tested in detail.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 5 '08 #2

"RolfK" <Ro*********@eu.necel.comwrote in message
news:e1**********************************@a3g2000p rm.googlegroups.com...
Dear ALL,

I have to convert a string of "10101010111"to a decimal number.

What is the standard solution in XSLT2.0 ?

Thanks a lot

RolfK
This is trivial using FXSL.

Below are three solutions, one using, respectively, the functions
f:zipWith(), f:foldl() and f:str-foldl():

testFunc-zipWithBits.xsl:
=================
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs"
>
<xsl:import href="../f/func-zipWithDVC.xsl"/>
<xsl:import href="../f/func-Operators.xsl"/>

<xsl:output method="text"/>

<xsl:variable name="vBits" as="xs:string" select="'10101010111'"/>

<xsl:variable name="vZeroBits" as="xs:string"
select="'00000000000000000000000000000000000000'"/>

<xsl:variable name="vbinPowers" as="xs:integer+"
select="1,2,4,8,16,32,64,128,256,512,1024,2048,409 6,8192,16384,32768"/>

<xsl:template match="/">

<xsl:sequence select=
"sum(f:zipWith(f:mult(),
$vbinPowers,
reverse(f:zipWith(f:subtr(),
string-to-codepoints($vBits),
string-to-codepoints($vZeroBits)
)
)
)
)"
/>
</xsl:template>
</xsl:stylesheet>
testFun-foldlBits.xsl:
==============
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f xs"
>
<xsl:import href="../f/func-foldl.xsl"/>
<xsl:import href="../f/func-zipWith.xsl"/>
<xsl:import href="../f/func-Operators.xsl"/>

<xsl:output method="text"/>

<xsl:variable name="vBits" as="xs:string" select="'10101010111'"/>

<xsl:variable name="vZeroBits" as="xs:string"
select="'00000000000000000000000000000000000000'"/>

<xsl:template match="/">

<xsl:variable name="vintBits" as="xs:integer*"
select="f:zipWith(f:subtr(),
string-to-codepoints($vBits),
string-to-codepoints($vZeroBits)
)"
/>

<xsl:sequence select=
"f:foldl(f:binAccum(), 0, $vintBits)"
/>
</xsl:template>

<xsl:function name="f:binAccum" as="xs:integer">
<xsl:param name="arg1" as="xs:integer"/>
<xsl:param name="arg2" as="xs:integer"/>

<xsl:sequence select="2*$arg1 + $arg2"/>
</xsl:function>

<xsl:function name="f:binAccum" as="element()">
<f:binAccum/>
</xsl:function>

<xsl:template match="f:binAccum" mode="f:FXSL">
<xsl:param name="arg1" as="xs:integer"/>
<xsl:param name="arg2" as="xs:integer"/>

<xsl:sequence select="f:binAccum($arg1,$arg2)"/>
</xsl:template>
</xsl:stylesheet>

testFunc-str-foldlBits.xsl:
==================
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="f xs"
>
<xsl:import href="../f/func-str-foldl.xsl"/>

<xsl:output method="text"/>

<xsl:variable name="vstrBits" as="xs:string"
select="'10101010111'"/>

<xsl:template match="/">
<xsl:sequence select="f:str-foldl(f:binAccum(), 0, $vstrBits)"/>
</xsl:template>

<xsl:function name="f:binAccum" as="xs:integer">
<xsl:param name="arg1"/>
<xsl:param name="arg2"/>

<xsl:sequence select="2*xs:integer($arg1) + xs:integer($arg2)"/>
</xsl:function>

<xsl:function name="f:binAccum" as="element()">
<f:binAccum/>
</xsl:function>

<xsl:template match="f:binAccum" mode="f:FXSL">
<xsl:param name="arg1"/>
<xsl:param name="arg2"/>

<xsl:sequence select="f:binAccum($arg1,$arg2)"/>
</xsl:template>
</xsl:stylesheet>
All three transformations above produce the correct result:
1367
Cheers,
Dimitre Novatchev
Nov 6 '08 #3
I wrote this 1.0 stylesheet to convert a string to base64
representation,

http://gandhimukul.tripod.com/style/base64encoder.xsl

In this stylesheet, there is a template,
binaryToDecimal

you can use this if you want.

A 2.0 solution will be a lot shorter, though.

Cheers,
Mukul

On Nov 5, 9:27*pm, RolfK <Rolf.Kem...@eu.necel.comwrote:
Dear ALL,

I have to convert a string of *"10101010111"to a decimal number.

What is the standard solution in XSLT2.0 ?

Thanks a lot

RolfK
Nov 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
9
by: Gaijinco | last post by:
Is there any way to declare a variable as a binary so that if the variable "var" holds the value of 1001, then ++var = 1010?
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: vendredi5h | last post by:
Hello all, I'd like to split a 64-bits word (hold as a floating point number) into two 32-bits words (hold as integers). Bitwise operators are working on Integers, not on floating point. ...
3
by: DustWolf | last post by:
Hello, I am wondering, what is the standard for including decimal numbers in XML code? What determines what is the decimal delimiter and what can be the grouping symbol? I have just realized...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.