You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • "salmanmanekia" is male
  • "salmanmanekia" started this thread

Posts: 85

Location: Finland

Occupation: Software Developement

  • Send private message

1

Tuesday, February 5th 2008, 3:07pm

conversion in binary,decimal and hex

Hi all,
I have written a set of code which is conversion of binary ,decimal and hexa..
the code works fine for conversion of a decimal number into binary and hex,but it doesnt work for binary and hex conversion...the code for dec conversion is

1, bool ok;
2, int num= newValue.toInt(&ok);
3, if (ok) {
4, hexEdit->setText(QString::number(num, 16));
5, binEdit->setText(QString::number(num, 2));
6, } else {
7, hexEdit->setText("");
8 ,binEdit->setText("");

if the same thing is done with hex in this way it doesnt work

1, bool ok;
2, int num= newValue.toInt(&ok);
3, if (ok) {
4, decEdit->setText(QString::number(num,10));
5, binEdit->setText(QString::number(num, 2));
6, } else {
7, binEdit->setText("");
8, decEdit->setText("");

i think the problem lies in line 4 or 5 .....any other function or idea would be helpful !!

2

Tuesday, February 5th 2008, 11:16pm

RE: conversion in binary,decimal and hex

what is newValue, a QString ???

give some example of what you obtain
Nicolas

  • "salmanmanekia" is male
  • "salmanmanekia" started this thread

Posts: 85

Location: Finland

Occupation: Software Developement

  • Send private message

3

Wednesday, February 6th 2008, 12:10am

yup, newValue is a QString,
when i put the hex value for eg ('a') in input field the decimal and binary are not shown,
when i input a dec value for eg ('10 ') the hex changes to ('a') and binary shows ('1010').this works fine
i have also make a function for converting binary numbers ,its also not working ,the code is

void ByteConverterDialog::binChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
//QString& newValue = QString::number(num1,2);
//int num= newValue1.toInt(&ok);
if (ok) {
decEdit->setText(QString::number(num, 10));
hexEdit->setText(QString::number(num, 16));
} else {
hexEdit->setText("");
decEdit->setText("");
}
}
the output it shows is when a binary number ('1010') is input then in decimal it shows '1010' and hex shows '3f2' which is basically taking '1010' as decimal and then converting it to hex ....!!!

4

Wednesday, February 6th 2008, 12:49am

try:

Source code

1
2
3
QString s = "100";
int     i = s.toInt( &ok, 16 );
qDebug() << i;

you should obtain
256

10 is the default base to scan the string
so, QString( "a" ).toInt( &ok ), results to an error
Nicolas

This post has been edited 2 times, last edit by "Nicolas SOUCHON" (Feb 6th 2008, 12:53am)


  • "salmanmanekia" is male
  • "salmanmanekia" started this thread

Posts: 85

Location: Finland

Occupation: Software Developement

  • Send private message

5

Wednesday, February 6th 2008, 12:03pm

wow ! it works....thnks nicholos ....