473,511 Members | 15,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert state of radiobutton => byte on send

I have asked a sortlike question on this group, but this is an other
approach for this problem.

I want to send information about a set of 8 radiobuttons to a device
that listens to TCP/IP on port 80.

I have 8 sets of radiobuttons, each has a on and off state.
When this form is send, I would like that the state of the buttons is
converted to a byte.

So button the on state of these buttons adds the following value to
the value that is send in the GET command.
Button 1 = 1
Button 2 = 2
Button 3 = 4
Button 4 = 8
Button 5 = 16
Button 6 = 32
Button 7 = 64
Button 8 = 128

So if button 1 and 8 are marked at the time the form is send, the
script adds the values of these buttons 1 + 128 = 129 and send this as
result of the form.

Can I do this easily?
Oct 18 '08 #1
2 2172
In comp.lang.javascript message <ptsjf417lo2sceejf8m19perf9dreu8bfm@4ax.
com>, Sat, 18 Oct 2008 16:40:54, te**@testnospam.nl posted:
>I have 8 sets of radiobuttons, each has a on and off state.
When this form is send, I would like that the state of the buttons is
converted to a byte.
>Can I do this easily?
Apparently not. But I can : consider

<formDEMO ONLY; lacks polish
<br>..0..1
<br><input type=radio name=x1><input type=radio name=x11
<br><input type=radio name=x2><input type=radio name=x22
<br><input type=radio name=x3><input type=radio name=x34
<br><input type=radio name=x4><input type=radio name=x48
<br><input type=radio name=x5><input type=radio name=x516
<br><input type=radio name=x6><input type=radio name=x632
<br><input type=radio name=x7><input type=radio name=x764
<br><input type=radio name=x8><input type=radio name=x8128
<br>&nbsp; <input type=button value="?" onClick="Wot(this.form)">

<script>
function Wot(F) { var X=0, J=9
while (--J) X = X*2 + F["x"+J][1].checked
alert(X) }
</script>

There, F["x"+J] refers to a radiobutton pair.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 18 '08 #2
On Oct 18, 4:40*pm, t...@testnospam.nl wrote:

Here's something to start playing with:

-Copy paste it and edit the line deviceURL= "http://0.0.0.0/headers?"
to point to the ip of your device.
-save it as utf8.
-name it whatever.html.
-open it in the browser (better not IE).
-click the bits buttons to toggle them between 0/1
-click send and an http GET request will be sent to your device:
something like :
http://0.0.0.0/headers?&rnd=6808913279445241&data=fe
-in the device, extract from the url the "&data=xx" part and ignore
the rest.
-the bits are sent in text as hex: the last 2 chars of the url.
-feel free to ask any questions.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Binary & buttons</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="jorge">
<!-- Date: 2008-10-19 -->
<style>
button { width: 50px; height: 35px; }
.sendBtn { width: 90px; height: 35px; }
</style>
<script>
window.onload= function (event) {
var i= 0, max= 7, d= document, bits= [], e,
sendBtn= d.createElement('button');

do {
e= bits[i]= d.createElement('button');
e.bit= 1;
e.weight= Math.pow(2, max-i);
(e.onclick= bitClick).apply(e);
d.body.appendChild(e);
d.body.appendChild(d.createTextNode(i<max ? "+" : "="));
} while (++i <= max)

d.body.appendChild(sendBtn);
sendBtn.className= "sendBtn";
sendBtn.onclick= function () {
var e, deviceURL= "http://0.0.0.0/headers?",
rnd= "&rnd="+ (""+Math.random()).substr(2);

e= d.createElement('img');
e.src= deviceURL+ rnd+ "&data="+ sendBtn.hex;
d.body.appendChild(d.createTextNode(e.src));
d.body.appendChild(e);
d.body.appendChild(d.createElement('br'));
};

d.body.appendChild(d.createElement('br'));

function bitClick (event) {
var i= bits.length, hex= 0;
this.bit= +(!this.bit);
this.innerHTML= this.bit*this.weight;
while (--i >= 0) {
hex+= +bits[i].innerHTML;
}
if ((sendBtn.hex= hex.toString(16)).length === 1) {
sendBtn.hex= "0"+ sendBtn.hex;
}
sendBtn.innerHTML= "send: 0x"+sendBtn.hex;
}
};
</script>
</head>
<body>

</body>
</html>
--
Jorge

"Silly things such as logic, common sense and rational thoughts are
not allowed in this forum."
Oct 19 '08 #3

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

Similar topics

2
2929
by: Google Mike | last post by:
I've got pyGTK 2-1.99.14 (which comes with my RH9 Linux). Does anyone know how to set the gtk.RadioButton font? I'm finding the font size too big for an application I need to build.
4
8700
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
3
2395
by: Fred Palmer | last post by:
I am trying to get reponses from multiple devices on my network by sending the following message via UDP: // Create the message Byte bytesSent = new Byte {0x00, 0x00, 0x00, 0xF6}; Socket s =...
14
29303
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
1
1601
by: Jim | last post by:
Here is a snipet of the code. // State object for receiving data from remote device. public class StateObject { // Client socket. public Socket workSocket = null; // Size of receive buffer....
2
11157
by: Joey Lee | last post by:
Hi, Does anyone know how I am able to write a utf-8 encoded binary string into binary file? Currently I am given a UTF-8 string which was read from a gif image. Here are my functions... ...
0
4253
by: vinothgsd | last post by:
Hi, I have created Client/Server application. In that Client side Program i have used Asynchronous Callback method for send and receive the stream. In the Receive method I want to make the...
6
5238
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
1
2213
by: ismailc | last post by:
Hi, I don't know xslt - i just need a modification done to existing xslt file. At the moment the page has 2 radio buttons with a submit button, which exeutes the code dependant on the radio...
0
7148
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...
1
7089
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
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.