Question convert hex to number

Joined
Mar 1, 2011
Messages
14
Programming Experience
Beginner
i got hex number from scale weight
example:"90 00 00 01 34 = 180 kg" or "90 00 03 03 7c=49670 kg"
how to convert hex to number(integer)
thank you
 
What EXACTLY do you have? Do you have a String containing the hexadecimal representation of a number or do you have a Byte array?

As for the conversion, you're going to have to consult the documentation for the scale to find out exactly what those values mean because 0x134 is not 180 and 0x3037C is not 49670. Once you know how the conversion is supposed to be done, then you can write code to do it.
 
in scale document:
byte 0: 1 X X M Z DP2 DP1 DP0
byte 1: 0 X OR OC SG W23 W22 W21
byte 2: 0 W20 W19 W18 W17 W16 W15 W14
byte 3: 0 W13 W12 W11 W10 W9 W8 W7
byte 4: 0 W6 W5 W4 W3 W2 W1 W0
and
i recive 5byte like(90 00 00 01 34) and show 180kg on scale weigh.
please help me
sorry for my bad english
 
Serial Data Stream Description:
You receive 5 Bytes of data, having 7 bits of useable data
and 1 MSB (right bit for synchronization):
3-1)

The First byte:
Example: 0x90 or 90 hex
Starting with MSB (right bit) of 1 for Starting Byte of 5 bytes frame
< When you receive one Byte from COM port and its MSB is 1,
This is the First byte. >

[Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0]
1 X X M Z DP2 DP1 DP0
X: Dont care
M: 0=Motion
Z: 1=Zero
DP2 DP1 DP0: 3 bit decimal point for weight
3-2)

The second byte:
<This Byte has MSB 0>
[Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0]
0 W23 W22 W21
3-3)

The third byte:
<This Byte has MSB 0>
[Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0]
0 W20 W19 W18 W17 W16 W15 W14
3-4)

The forth byte:
<This Byte has MSB 0>
[Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0]
0 W13 W12 W11 W10 W9 W8 W7
3-5)

The fifth byte:
<This Byte has MSB 0>
[Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0]
0 W6 W5 W4 W3 W2 W1 W0
Place these bits in Long variable, this is the real Displayed Weight in
TEC Weighing Indicator:
Weight=W[23..0]=W23W22W21W20W19W18W17W16W15W14W13W12W11W10W9W8W7W6W5W4W3W2W1W0
DecimalPoint=DP[2..0]=DP2DP1DP0;


Outdoor 3000 Samples:
P+ 01994
@+ 01990
@+ 01993
p+ 00000
I>>>>>>>
@- 01994
@-01,994
p+00,000
P+01,994
all packets ended with Enter character with ascii code=0x0d
Line2: stable weight no decimal point {P+ 01994}
Line3 and Line4: varying weight(motion) {@+ 01990} and {@+ 01993}
Line5: zero stable weight {p+ 00000}
Line6: Error {I>>>>>>>}
Line7: negative weight {@- 01994}
Line8: negative weight + decimal point=3 {@-01,994}
Line9: zero stable weight + decimal point=3 {p+00,000}
Line10: stable weight + decimal point=3 {P+01,994}
default RS232 serial port Settings : 9600,8,E,2

i found this information in document
can u help me?
 
below code write with c++ and save the weight in textfile but i dont work with c++

#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
#include"dos.h"
#include"conio.h"
#include"bios.h"
#include"time.h"

unsigned char data[32768];

int rx_ch(int port);
void tx_ch(int port,unsigned char ch);

void main(int argc,char *argv[])
{
unsigned int i,ch,chi,flag,byte2,count,checksum;
int end_flag,error_code,dp,state;
long weight;
double j;
int port,status,ser_counter,T0,T1;
FILE *fp;
char filename[13];
unsigned int module_beg,module_length;

port=0;
bioscom(0,0xfb,port); /* 9600 baud , even parity , 1 stop bit , 8 bit data */

clrscr();
end_flag=0;

while(!end_flag)
{
state=0;

do ch=rx_ch(port);
while( (ch!=1000) && ((ch&0x80)==0) );
if(ch==1000) end_flag=1;

if(!end_flag)
{
flag=ch;
dp=ch&0x07;
}
if(!end_flag)
{
ch=rx_ch(port);
if(ch==1000) end_flag=1;
if(ch&0x80)state=0;
else { weight=ch&0x03; byte2=ch; state=1; }
}
if( (!end_flag) && (state==1) )
{
ch=rx_ch(port);
if(ch==1000) end_flag=1;
else if(ch&0x80)state=0;
else { weight = (weight<<7) + (ch&0x7f); state=2; }
}
if( (!end_flag) && (state==2) )
{
ch=rx_ch(port);
if(ch==1000) end_flag=1;
else if(ch&0x80)state=0;
else { weight = (weight<<7) + (ch&0x7f); state=3; }
}
if( (!end_flag) && (state==3) )
{
ch=rx_ch(port);
if(ch==1000) end_flag=1;
else if(ch&0x80)state=0;
else
{
weight = (weight<<7) + (ch&0x7f);
if(byte2&0x08)
weight = -weight;
state=4;
}
}
if( (!end_flag) && (state==4) )
{
fp=fopen("out.txt", "wt");
gotoxy(10,10);
if(flag&0x10){ printf(" ");fprintf(fp," ");}
else{printf("M");fprintf(fp,"M");}
gotoxy(10,12);
if(flag&0x08){printf("Z");fprintf(fp,"Z");}
else {printf(" ");fprintf(fp," ");}

j=1;
for(i=0;i<dp;i++)
j*=10;
gotoxy(12,11); printf(" ");fprintf(fp," ");
gotoxy(12,11);
if( ((byte2&0x10)==0) && ((byte2&0x20)==0) )
{printf("%10lf ",(double)(weight)/j);
fprintf(fp,"%10lf ",(double)(weight)/j);
}else
{printf("--------- ");
fprintf(fp,"--------- ");
}
fclose(fp);
end_flag=1;
}
}
}

//---------------------------------------------------------------------------
int rx_ch(int port)
{
int status,keyhit=0;

do{
status = 0X100 & bioscom(3,0,port);
if(!status) status=keyhit=kbhit();
}
while( !status );

if(keyhit) return(1000);
else return( 0xff & bioscom(2,0,port) );
}
//---------------------------------------------------------------------------
void tx_ch(int port,unsigned char ch)
{
// printf("%3d ",ch);
bioscom(1,ch,port);
}
 
VB.NET:
        Dim MeasuredWeightInFullKilograms As Long = 0

        Dim BitRepresentation As String = String.Empty
        BitRepresentation &= String.Format("{0,7}", Convert.ToString(&H3, 2)).Replace(" ", "0")
        BitRepresentation &= String.Format("{0,7}", Convert.ToString(&H3, 2)).Replace(" ", "0")
        BitRepresentation &= String.Format("{0,7}", Convert.ToString(&H7C, 2)).Replace(" ", "0")

        MeasuredWeightInFullKilograms = BinaryToLong(BitRepresentation)

        MessageBox.Show(MeasuredWeightInFullKilograms.ToString)

The BinaryToLong function comes from VB Helper: HowTo: Convert values between decimal, hexadecimal, octal, and binary in VB .NET
 
Back
Top