little endian

C

CTG

var x = "B113769A"

How do you convert B113769A to "9A7613B1" to get the proper
conversion ?
 
R

RobG

var x = "B113769A"

How do you convert B113769A to "9A7613B1" to get the proper
conversion ?

I guess you want to just reverse the order of the pairs, I'm sure
there are many algorithms - here's one:


function swapEndianHex(x)
{
// Make sure it's a string
x = String(x);

// Return if has invalid characters
if (/[^a-f0-9]/.test(x.toLowerCase())) return;

// Might need left padding
x = (x.length % 2)? '0'+x : x;

// Split into an array of pairs, reverse and join again
return x.match(/../g).reverse().join('');
}


Depending on your requirements, you might be able to remove some or
all of the first 3 statements that do validation.
 
E

Evertjan.

RobG wrote on 22 mei 2008 in comp.lang.javascript:
// Return if has invalid characters
if (/[^a-f0-9]/.test(x.toLowerCase())) return;

if ( /[^a-f0-9]/i.test(x) ) return;
// Might need left padding
x = (x.length % 2)? '0'+x : x;

// Split into an array of pairs, reverse and join again
return x.match(/../g).reverse().join('');

Nice! Will remember that one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top