Sponsored Content
Operating Systems OS X (Apple) Unsigned to signed, error?... Post 302904967 by wisecracker on Sunday 8th of June 2014 03:10:54 PM
Old 06-08-2014
Unsigned to signed, error?...

Hi guys...
Macbook Pro, 13", circa August 2012, OSX 10.7.5, default bash terminal.

I require the capability to convert +32767 to -32768 into signed hex words...
The example piece code below works perfectly except...
Code:
#/bin/bash
# sign.sh
# Unsign to sign...
while true
do
	# I have used $(( ~n )), where n is a positive integer too.
	# Test piece only hence no error checking at all.
	read -p "Enter negative number -1 to -32768:- " num
	if [ $num -le -1 ]
	then
		printf "$[ ( $num + 65536 ) ]\n"
	else
		printf "Zero or positive value entered, $num.\n"
	fi
	# Why a 64 bit _hex_ value here?
	hex=$(printf "%04X\n" $num)
	printf "$hex\n"
	# I have to use this to correct.
	if [ ${#hex} -gt 4 ]
	then
		hex="${hex:$[ ( ${#hex} - 4 ) ]:4}"
	fi
	printf "$hex\n"
done

Results:-
Code:
Last login: Sun Jun  8 19:52:33 on ttys000
AMIGA:barrywalker~> ./sign.sh
Enter negative number -1 to -32768:- 0
Zero or positive value entered, 0.
0000
0000
Enter negative number -1 to -32768:- 1
Zero or positive value entered, 1.
0001
0001
Enter negative number -1 to -32768:- -1
65535
FFFFFFFFFFFFFFFF
FFFF
Enter negative number -1 to -32768:- 32767
Zero or positive value entered, 32767.
7FFF
7FFF
Enter negative number -1 to -32768:- -32768
32768
FFFFFFFFFFFF8000
8000
Enter negative number -1 to -32768:- ^CAMIGA:barrywalker~> 
AMIGA:barrywalker~> _

My question is this:-
Why does the line hex=$(printf "%04X\n" $num) not truncate the hex
string to 4 characters?
I have tried, %04X, %02X and %X along with $num and "$num" but I still have to use the extra code to correct to 4 hex characters.

TIA...
 

10 More Discussions You Might Find Interesting

1. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies

2. Programming

to get the correct value with unsigned int

hi, Please help me with the following code to get the difference in values. struct a{ int b1; int c1; char d1; } main() { unsigned int b=10; unsigned int c; c = b - (unsigned int )sizeof(a); printf("%d",c); } Here c returns some junk value. How can i get the... (2 Replies)
Discussion started by: naan
2 Replies

3. UNIX for Dummies Questions & Answers

So, like, I signed on with a new hosting company...

... and there was absolutely nothing installed except fedora and ssh. I used yum to install vsftp and httpd, both start and ps shows they're running, and yet I can't connect with either of them. Where on earth or in redhat do I begin looking to unravel this one? I've overseen a server before but... (3 Replies)
Discussion started by: Bobby
3 Replies

4. Red Hat

cast from const void* to unsigned int loses precision

Hello everey one, here i am attempting to compile a c++ project .it's throughing the following errors. my machine details are as follows: Linux chmclozr0119 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux errors: ===== Generating... (0 Replies)
Discussion started by: mannam srinivas
0 Replies

5. Shell Programming and Scripting

add signed and unsigned numbers- awk help

Hi All, I have written the below to add the numbers in a column. Postive numbers are unsigned and negative numbers are signed in the file. After the below cmd I am getting -0.00 , instead of 0.00. Can someone guide me on what I am missing in the cmd. grep '^L' $FileName| awk -F"|" ' {... (7 Replies)
Discussion started by: gsjdrr
7 Replies

6. UNIX for Advanced & Expert Users

"Signed Linux" - Only executing signed programs

Hey folks, not sure whether this or the security board is the right forum. If I failed, please move :) So here's the problem: I need to build a Linux environment in which only "signed" processes are allowed to run. When I say signed I don't mean a VeriSign signature like you know it from... (5 Replies)
Discussion started by: disaster
5 Replies

7. Programming

Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables?? When I declare a variable "unsigned;" what did I do it??? Why would I delcare an integer "long" or "short" ( unsigned or signed)?? Any examples of when things like "unsigned", "long", "short" etc...... (6 Replies)
Discussion started by: cpp_beginner
6 Replies

8. Programming

Comparing unsigned char bits.

/******************************************************************************/ /* Printing an unsigned character in bits */ #include <stdio.h> void display_bits ( unsigned char ); int main() { unsigned char x; /*... (15 Replies)
Discussion started by: robin_simple
15 Replies

9. Programming

Signed and unsigned intergers

when a date type is considered signed and unsigned is that simple referring to - for signed and positive numbers for unsigned? Further if that is the case would mutiplying and dividing ect where 2 signed numbers, like (-2)*(-2) = 4 result in a unsigned. (3 Replies)
Discussion started by: Fingerz
3 Replies

10. Programming

Unable to assign zero to unsigned character array

Hi, I am unable to assign value zero to my variable which is defined as unsigned char. typedef struct ABCD { unsigned char abc; unsigned char def; unsigned char ghi; } ABCD; typedef ABCD *PABCD; In my Por*C code, i assign the values using memcpy like below ... (3 Replies)
Discussion started by: gthangav
3 Replies
mlib_ImageColorHSL2RGB(3MLIB)				    mediaLib Library Functions				     mlib_ImageColorHSL2RGB(3MLIB)

NAME
mlib_ImageColorHSL2RGB - HSL to RGB color conversion SYNOPSIS
cc [ flag... ] file... -lmlib [ library... ] #include <mlib.h> mlib_status mlib_ImageColorHSL2RGB(mlib_image *dst, const mlib_image *src); DESCRIPTION
The mlib_ImageColorHSL2RGB() function performs a conversion from hue/saturation/lightness to red/green/blue. The source and destination images must be three-channel images. It uses the following equations: L' = L if L <= 1/2 L' = 1 - L if L > 1/2 V = L + S*L' P = L - S*L' Q = L + S*L'*(1 - 2*fraction(H*6)) T = L - S*L'*(1 - 2*fraction(H*6)) R, G, B = V, T, P if 0 <= H < 1/6 R, G, B = Q, V, P if 1/6 <= H < 2/6 R, G, B = P, V, T if 2/6 <= H < 3/6 R, G, B = P, Q, V if 3/6 <= H < 4/6 R, G, B = T, P, V if 4/6 <= H < 5/6 R, G, B = V, P, Q if 5/6 <= H < 1 where 0 <= H < 1 and 0 <= S, L, L', V, P, Q, T, R, G, B <= 1. Assuming a pixel in the source image is (h, s, l) and its corresponding pixel in the destination image is (r, g, b), then for MLIB_BYTE images, the following applies: H = h/256 S = s/255 L = l/255 r = R*255 g = G*255 b = B*255 for MLIB_SHORT images, the following applies: H = (h + 32768)/65536 S = (s + 32768)/65535 L = (l + 32768)/65535 r = R*65535 - 32768 g = G*65535 - 32768 b = B*65535 - 32768 for MLIB_USHORT images, the following applies: H = h/65536 S = s/65535 L = l/65535 r = R*65535 g = G*65535 b = B*65535 and for MLIB_INT images, the following applies: H = (h + 2147483648)/4294967296 S = (s + 2147483648)/4294967295 L = (l + 2147483648)/4294967295 r = R*4294967295 - 2147483648 g = G*4294967295 - 2147483648 b = B*4294967295 - 2147483648 PARAMETERS
The function takes the following arguments: dst Pointer to destination image. src Pointer to source image. RETURN VALUES
The function returns MLIB_SUCCESS if successful. Otherwise it returns MLIB_FAILURE. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
mlib_ImageColorHSL2RGB_Fp(3MLIB), mlib_ImageColorRGB2HSL(3MLIB), mlib_ImageColorRGB2HSL_Fp(3MLIB), attributes(5) SunOS 5.10 10 Nov 2004 mlib_ImageColorHSL2RGB(3MLIB)
All times are GMT -4. The time now is 11:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy